LeetCode Problem

How to Solve Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values

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.

GhostInterview Help

Need help with Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values without spending extra time grinding it?

GhostInterview can read Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3572Array scanning plus hash lookupReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

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

MetricValue
TimeDepends on the final approach
SpaceDepends 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

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.

GhostInterview Solver

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.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.