Maximum Product of Two Elements in an Array

🏠 ⬅️ ➡️

Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1).

Example 1:

Input: nums = [3,4,5,2] Output: 12 Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12.

Example 2:

Input: nums = [1,5,4,5] Output: 16 Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.

Example 3:

Input: nums = [3,7] Output: 12

Constraints:

  • 2 <= nums.length <= 500
  • 1 <= nums[i] <= 10^3

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 500
    integer, dimension(n) :: nums
    integer :: i, j, max_val

    ! Example 1
    nums = [3, 4, 5, 2]
    call solve(nums, i, j, max_val)
    write (*, '(A, I0, A, I0, A, I0)') &
        'Example 1: ', nums(i), ' * ', nums(j), ' = ', max_val

    ! Example 2
    nums = [1, 5, 4, 5]
    call solve(nums, i, j, max_val)
    write (*, '(A, I0, A, I0, A, I0)') &
        'Example 2: ', nums(i), ' * ', nums(j), ' = ', max_val

    ! Example 3
    nums = [3, 7]
    call solve(nums, i, j, max_val)
    write (*, '(A, I0, A, I0, A, I0)') &
        'Example 3: ', nums(i), ' * ', nums(j), ' = ', max_val

contains

    subroutine solve(nums, i, j, max_val)
        implicit none
        integer, dimension(:), intent(in) :: nums
        integer, intent(out) :: i, j, max_val

        integer :: ii, jj, val

        do ii = 1, size(nums) - 1
            do jj = ii + 1, size(nums)
                val = (nums(ii) - 1) * (nums(jj) - 1)
                if (val > max_val) then
                    max_val = val
                    i = ii
                    j = jj
                end if
            end do
        end do
    end subroutine solve

end program main
Compiled
Executed
Correct
module MaxProductOfTwoElements
    implicit none
    private
    public :: max_product

contains

    function max_product(nums) result(max_prod)
        integer, intent(in) :: nums(:)
        integer :: max_prod, i, j, prod

        max_prod = 0
        do i = 1, size(nums) - 1
            do j = i + 1, size(nums)
                prod = (nums(i) - 1) * (nums(j) - 1)
                if (prod > max_prod) then
                    max_prod = prod
                end if
            end do
        end do
    end function max_product
end module MaxProductOfTwoElements

program test_max_product
    use MaxProductOfTwoElements
    implicit none
    integer :: nums(4)

    nums = [3, 4, 5, 2]
    write (*, '(A, I0)') 'Max product of two elements in nums = ', &
                         max_product(nums)

    nums = [1, 5, 4, 5]
    write (*, '(A, I0)') 'Max product of two elements in nums = ', &
                         max_product(nums)

    nums = [3, 7]
    write (*, '(A, I0)') 'Max product of two elements in nums = ', &
                         max_product(nums)
end program test_max_product
🌐 Data from online sources
from collections import Counter

def minSetSize(arr):
    count = Counter(arr)
    freq = list(count.values())
    freq.sort(reverse=True)

    removed, setSize = 0, 0
    for f in freq:
        removed += f
        setSize += 1
        if removed * 2 >= len(arr):
            return setSize

    return 0
1. Create a map (or dictionary) to store the count of each unique integer in the input array.
  1. Iterate through the input array, incrementing the count for each number in the map.
  2. Create an array (or list) of the counts in the map, sorted in descending order.
  3. Initialize variables removed and setSize to zero. removed tracks the number of elements removed so far, and setSize is the size of the set of integers.
  4. Iterate through the sorted count array, adding the count to the removed variable and incrementing setSize. If the number of removed elements is at least half the size of the input array, return setSize.
  5. If the loop completes without returning, return 0.
🌐 Data from online sources
#include <unordered_map>
#include <vector>
#include <algorithm>

int minSetSize(std::vector<int>& arr) {
    std::unordered_map<int, int> count;
    for (auto value : arr)
        count[value]++;

    std::vector<int> freq;
    for (auto& entry : count)
        freq.push_back(entry.second);

    std::sort(freq.rbegin(), freq.rend());

    int removed = 0, setSize = 0;
    for (auto f : freq) {
        removed += f;
        setSize++;
        if (removed * 2 >= arr.size())
            return setSize;
    }

    return 0;
}
1. Create a map (or dictionary) to store the count of each unique integer in the input array.
  1. Iterate through the input array, incrementing the count for each number in the map.
  2. Create an array (or list) of the counts in the map, sorted in descending order.
  3. Initialize variables removed and setSize to zero. removed tracks the number of elements removed so far, and setSize is the size of the set of integers.
  4. Iterate through the sorted count array, adding the count to the removed variable and incrementing setSize. If the number of removed elements is at least half the size of the input array, return setSize.
  5. If the loop completes without returning, return 0.