Maximum Product Pair With Target Sum - Solution & Explanation
Problem Statement
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āāāāāāā
Approach Overview
Problem Overview: Given an array of integers and a target sum, find a pair of elements at different indices whose values add up to the target. Among all valid pairs, return the maximum product. If no pair exists, return -1.
Approach 1: Brute Force Pair Check (O(n²) time, O(1) space)
Use two nested loops. The outer loop fixes index i, and the inner loop scans every j > i. For each pair, check whether nums[i] + nums[j] == target. If it matches, compute the product and update the running maximum. This approach needs no extra memory, and it works on unsorted input. Use it to confirm your logic, but do not submit it as the final answer when n is large.
Approach 2: Sort and Two Pointers (O(n log n) time, O(1) extra space)
Sort the array. Place one pointer at the start and one at the end. If the sum is less than the target, move the left pointer right. If the sum is greater, move the right pointer left. When the sum matches, record the product and move both pointers inward. Every candidate pair is visited once after sorting, so the scan is linear. The sort dominates the cost. This is the right choice when you are already working with sorted data or when memory must stay constant. See more on two pointers and sorting.
Approach 3: Hash Map Complement Lookup (O(n) time, O(n) space)
Walk the array once and store each value in a hash set or hash map as you go. For each element x, compute the complement target - x and check whether it already exists in the set. If it does, the pair (x, target - x) is valid, and its product is x * (target - x). Keep the maximum product seen. Each lookup takes average O(1), so the whole pass runs in O(n). Count occurrences when a value equals its own complement, because you need two distinct indices for that case. This is the approach that scales to large inputs. Learn more about hash tables.
Recommended for interviews: Interviewers expect the hash map solution. Start with the brute force to show you understand the pair condition. Then move to the hash map to show you can cut the cost from quadratic to linear. Mention the sort and two pointer option if the interviewer asks about memory limits or sorted input.
Solution
Thinking
With
n\le 100, checking every pair of indices is already fast enough. Negative values need separate care: once the sum is fixed, moving the two numbers closer totarget/2does not make the product larger, so each product is compared directly.The complement of
xis alwaysy=target-x. A valid pair also requires the two values to be different, so the casex=ycan be skipped.The product depends only on those two values. While scanning from left to right, a hash map stores the latest index of each value; when
xis seen, look upyand keep the pair with the larger product.
Use a hash map pos to store the latest index of each value. Scan nums[i]=x from left to right and set y=target-x. If xneq y, y has already occurred, and xcdot y is greater than the best product so far, update the answer and place the larger value first so that nums[i]>nums[j]. If no valid pair is found, the answer stays [-1,-1].
The time complexity is O(n) and the space complexity is O(n), where n is the length of nums.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Check | O(n²) | O(1) | Small inputs or verifying logic |
| Sort and Two Pointers | O(n log n) | O(1) extra | Memory constrained or array already sorted |
| Hash Map Complement Lookup | O(n) | O(n) | General case and interview default |
Video Solutions (3)
Maximum Product Pair With Target Sum | LeetCode Biweekly Contest 193 | C++ Solution
Frequently Asked Questions
Is Maximum Product Pair With Target Sum easy or hard?
Maximum Product Pair With Target Sum Python solution?
How to solve Maximum Product Pair With Target Sum in O(n)?
What is the best approach for Maximum Product Pair With Target Sum?
What data structure is used in Maximum Product Pair With Target Sum?
What is the time complexity of Maximum Product Pair With Target Sum?
Is Maximum Product Pair With Target Sum asked at Google, Amazon, or Meta?
Ready to solve this problem?
Practice Maximum Product Pair With Target Sum with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor