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
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
temp.f95:9:4: 9 | nums = [3, 4, 5, 2] | 1 Error: Different shape for array assignment at (1) on dimension 1 (500 and 4) temp.f95:15:4: 15 | nums = [1, 5, 4, 5] | 1 Error: Different shape for array assignment at (1) on dimension 1 (500 and 4) temp.f95:21:4: 21 | nums = [3, 7] | 1 Error: Different shape for array assignment at (1) on dimension 1 (500 and 2)
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
temp.f95:37:4: 37 | nums = [3, 7] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 2)
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.
removed
and setSize
to zero. removed
tracks the number of elements removed so far, and setSize
is the size of the set of integers.removed
variable and incrementing setSize
. If the number of removed
elements is at least half the size of the input array, return setSize
.#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.
removed
and setSize
to zero. removed
tracks the number of elements removed so far, and setSize
is the size of the set of integers.removed
variable and incrementing setSize
. If the number of removed
elements is at least half the size of the input array, return setSize
.