Maximum Product Pair With Target Sum Problem
Description
LeetCode Problem 4074.
You are given an integer array nums and an integer target.
A pair of distinct indices (i, j) is valid if:
nums[i] + nums[j] == targetnums[i] > nums[j]
Return a valid pair [i, j] whose product nums[i] * nums[j] is maximum among all valid pairs. If no valid pair exists, return [-1, -1].
If multiple valid pairs achieve the maximum product, you may return any of them.
Example 1:
Input: nums = [1,2,3,4], target = 5
Output: [2,1]
Explanation:
There are 2 valid pairs:
No. (i, j) nums[i] nums[j] Sum Product
1 (3, 0) 4 1 5 4
2 (2, 1) 3 2 5 6
Both pairs sum to 5 and satisfy nums[i] > nums[j]. The second pair has the larger product, 3 * 2 = 6, so the answer is [2, 1].
Example 2:
Input: nums = [-3,-1,4,2], target = 1
Output: [3,1]
Explanation:
There are 2 valid pairs:
No. (i, j) nums[i] nums[j] Sum Product
1 (2, 0) 4 -3 1 -12
2 (3, 1) 2 -1 1 -2
Since -2 > -12, the pair at indices (3, 1) is chosen and the answer is [3, 1].
Example 3:
Input: nums = [3,3,5], target = 6
Output: [-1,-1]
Explanation:
No valid pair exists, since nums[0] and nums[1] sum to 6 but are equal.
Constraints:
2 <= nums.length <= 100-100 <= nums[i], target <= 100
Solution
Approach: Hash map
Scan the array and keep a map from each value to its latest index. For the current value x the partner must be y = target - x. A pair is only valid when x != y (we need nums[i] > nums[j]), and y must have appeared earlier. If x * y beats the best product so far, record the pair, putting the larger value first so that nums[i] > nums[j].
Pairs found this way cover every valid pair of values, because each pair is seen when its second element is scanned. If nothing is found, the answer stays [-1, -1].
Complexity: O(n) time and O(n) extra space.
from math import inf
class Solution:
def maxProductPair(self, nums: List[int], target: int) -> List[int]:
pos = {}
mx = -inf
ans = [-1, -1]
for i, x in enumerate(nums):
y = target - x
if x != y and x * y > mx and y in pos:
j = pos[y]
mx = x * y
ans = [i, j] if x > y else [j, i]
pos[x] = i
return ans