Difficulty: 🟢 Easy
The product difference between two pairs (a, b)
and (c, d)
is defined as (a * b) - (c * d)
.
- For example, the product difference between
(5, 6)
and(2, 7)
is(5 * 6) - (2 * 7) = 16
. Given an integer arraynums
, choose four distinct indicesw
,x
,y
, andz
such that the product difference between pairs(nums[w], nums[x])
and(nums[y], nums[z])
is maximized. Return the maximum such product difference.
Example 1:
Input: nums = [5,6,2,7,4]
Output: 34
Explanation: We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).
The product difference is (6 * 7) - (2 * 4) = 34.
Example 2:
Input: nums = [4,2,5,9,7,4,8]
Output: 64
Explanation: We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4).
The product difference is (9 * 8) - (2 * 4) = 64.
4 <= nums.length <= 104
1 <= nums[i] <= 104
Python3
class Solution:
def maxProductDifference(self, nums: List[int]) -> int:
min_1, min_2 = float("inf"), float("inf")
max_1, max_2 = float("-inf"), float("-inf")
for num in nums:
if num > max_1:
max_2 = max_1
max_1 = num
elif num > max_2:
max_2 = num
if num < min_1:
min_2 = min_1
min_1 = num
elif num < min_2:
min_2 = num
return max_1 * max_2 - min_1 * min_2
The solution aims to find the maximum product difference by identifying the four largest and two smallest elements in the nums
array. To do this, we maintain four variables to keep track of the two largest (max_1
and max_2
) and two smallest (min_1
and min_2
) elements as we iterate through the array.
Here's a step-by-step explanation of the solution:
-
Initialize
min_1
,min_2
,max_1
, andmax_2
to positive and negative infinity, respectively. These variables will be used to track the two smallest and two largest elements in thenums
array. -
Iterate through each element
num
in thenums
array.- Check if
num
is greater thanmax_1
. If it is, updatemax_2
tomax_1
andmax_1
tonum
. - If
num
is not greater thanmax_1
but is greater thanmax_2
, updatemax_2
tonum
. - Check if
num
is less thanmin_1
. If it is, updatemin_2
tomin_1
andmin_1
tonum
. - If
num
is not less thanmin_1
but is less thanmin_2
, updatemin_2
tonum
.
- Check if
-
After iterating through the entire array, calculate the maximum product difference using the two largest and two smallest elements:
max_product_difference = (max_1 * max_2) - (min_1 * min_2)
-
Return
max_product_difference
as the result.
-
Time Complexity: The solution iterates through the
nums
array once, resulting in a time complexity of O(n), where n is the length of the array. -
Space Complexity: The space complexity is O(1) because the solution uses a fixed number of variables to track the two largest and two smallest elements.
The solution efficiently finds the maximum product difference between two pairs of distinct elements in the nums
array by identifying the two largest and two smallest elements. It then calculates the product difference using these values and returns the result. The time complexity of the solution is linear in the size of the array, making it an efficient solution.
NB: If you want to get community points please suggest solutions in other languages as merge requests.