This problem requires selecting three distinct indices from array x to maximize the sum of corresponding y-values. Using a hash map to store the maximum y for each unique x ensures no redundant computations. Sorting the collected maximums allows quick selection of the top three values, providing an optimal O(n log n) solution while handling cases where fewer than three unique x-values exist.
Problem Statement
Given two integer arrays x and y of length n, choose three distinct indices i, j, k such that x[i], x[j], and x[k] are all different. Your task is to maximize y[i] + y[j] + y[k] among all valid triplets.
Return the maximum sum achievable under these conditions. If it is impossible to pick three distinct x-values, return -1. Focus on using array scanning with hash lookup to identify the top y-values for each unique x efficiently.
Examples
Example 1
Input: x = [1,2,1,3,2], y = [5,3,4,6,2]
Output: 14
Example 2
Input: x = [1,2,1,2], y = [4,5,6,7]
Output: -1
Constraints
- n == x.length == y.length
- 3 <= n <= 105
- 1 <= x[i], y[i] <= 106
Solution Approach
Use Hash Map to Track Maximum y per x
Iterate over the arrays and store each x with its maximum y in a hash map. Ignore any smaller y-values for the same x to prevent redundant calculations.
Collect and Sort Maximum y-values
Extract all maximum y-values from the hash map and sort them in descending order. Sorting enables quick selection of the largest three y-values corresponding to distinct x-values.
Return Top Three Sum or -1
If there are at least three unique x-values, sum the top three y-values from the sorted list and return it. Otherwise, return -1 to indicate no valid triplet exists.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n) for building the hash map and O(k log k) for sorting k unique x-values, where k <= n. Space complexity is O(k) for storing maximum y-values for each unique x in the hash map.
What Interviewers Usually Probe
- Ask for maximum sum with distinct x-values
- Check if candidate ignores duplicate x-values when selecting y
- Look for O(n) hash map preprocessing plus sorting for top three selection
Common Pitfalls or Variants
Common pitfalls
- Including multiple y-values for the same x, leading to invalid triplets
- Not handling fewer than three unique x-values, returning incorrect sum
- Sorting all y-values instead of only the maximums, increasing unnecessary computation
Follow-up variants
- Maximize sum with four distinct x-values instead of three
- Select triplet under a threshold constraint on y-values
- Minimize sum by picking distinct x-values instead of maximizing
How GhostInterview Helps
- Automatically identify maximum y for each unique x and eliminate redundant pairs
- Compute the top three y-values efficiently after hash preprocessing
- Handle edge cases like fewer than three unique x-values and return correct output
Topic Pages
Related GhostInterview Pages
- LeetCode Interview Copilot - Use GhostInterview as a live solver when you want direct help with LeetCode-style coding questions.
- Coding Interview Assistant - See how GhostInterview supports array, string, linked list, graph, and tree interview workflows.
- How GhostInterview Works - Review the screenshot, reasoning, and answer flow before using the solver in a live interview.
FAQ
What is the main pattern used in Maximize Y-Sum by Picking a Triplet of Distinct X-Values?
The core pattern is array scanning combined with hash lookup to track the maximum y for each unique x.
How do I handle duplicates in x while maximizing the y-sum?
Keep only the maximum y for each unique x in a hash map and ignore smaller y-values for the same x.
What should I return if fewer than three distinct x-values exist?
Return -1 because no valid triplet can be formed.
Is sorting all y-values necessary for this problem?
No, only the maximum y-values for each unique x need to be sorted, reducing unnecessary computation.
Can this approach scale to large arrays?
Yes, using a hash map to track maximum y-values ensures O(n) preprocessing and O(k log k) sorting, suitable for n up to 105.
Need direct help with Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values instead of spending more time grinding it?
Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.
Capture the prompt fast instead of rewriting the problem by hand.
Get the solution path, trade-offs, and complexity summary in one pass.
Stay outside captured layers on supported screen-share workflows.
Stay in the same pattern family
Maximize elegance of a k-length subsequence from a list of items with profits and categories.
Open problem page#3462 Maximum Sum With at Most K ElementsFind the maximum sum by selecting at most k elements from a 2D matrix respecting per-row limits using a greedy strategy.
Open problem page#3362 Zero Array Transformation IIIZero Array Transformation III requires removing the minimum number of queries to make all elements zero using greedy validation.
Open problem page