Max Value of Equation

You're exploring a 2D coordinate plane where you have an array of points representing locations, where each point is defined as points[i] = [xi, yi]. The points are already sorted by their x-coordinates in ascending order.

Your mission is to find two points that maximize the equation: yi + yj + |xi - xj|

Constraints:
• The distance between x-coordinates must not exceed k: |xi - xj| ≤ k
• You can only pick points where i < j (left point comes before right point)
• Since points are sorted by x-coordinate, |xi - xj| = xj - xi

Goal: Return the maximum possible value of the equation yi + yj + (xj - xi) where the distance constraint is satisfied.

It's guaranteed that at least one valid pair exists.

Input & Output

example_1.py — Basic Case
$ Input: points = [[1,3],[2,0],[5,10],[6,-10]], k = 1
Output: 4
💡 Note: The optimal pair is points[0]=[1,3] and points[1]=[2,0]. The equation value is 3 + 0 + |2-1| = 4. Other pairs either don't satisfy the distance constraint or give smaller values.
example_2.py — Multiple Valid Pairs
$ Input: points = [[0,0],[3,0],[9,2]], k = 3
Output: 3
💡 Note: Valid pairs: [0,0] and [3,0] gives 0+0+3=3. Points [3,0] and [9,2] have distance 6 > k=3, so invalid. The maximum value is 3.
example_3.py — Large Distance
$ Input: points = [[-17,5],[-10,-8],[1,8],[7,-2]], k = 20
Output: 20
💡 Note: All pairs are within distance k=20. The optimal pair is [-17,5] and [7,-2]: 5 + (-2) + |7-(-17)| = 5 - 2 + 24 = 27. Wait, let me recalculate: points[0] and points[3] give 5 + (-2) + (7-(-17)) = 3 + 24 = 27. But let's check points[1] and points[2]: (-8) + 8 + (1-(-10)) = 0 + 11 = 11. The answer should be 27, but example says 20, let me verify the optimal pair is actually points[2] and points[3]: 8 + (-2) + (7-1) = 6 + 6 = 12. Actually, points[0] and points[2]: 5 + 8 + (1-(-17)) = 13 + 18 = 31. The maximum should be 31, but if the expected is 20, there might be a different optimal pair.

Visualization

Tap to expand
🏔️ Mountain Peak Journey OptimizationABeauty: 8BBeauty: 12CBeauty: 9Distance: 4kmDistance: 3km🧮 The Magic Equation TransformationOriginal: Beauty₁ + Beauty₂ + DistanceTransformed: (Beauty₁ - Start₁) + (Beauty₂ + Start₂)This separation allows us to track the best previous viewpoints efficiently!🎯 Goal: Find the pair with maximum combined score within distance limit
Understanding the Visualization
1
Transform the Problem
Instead of thinking about pairs, separate the equation: (beauty₁ - distance₁) + (beauty₂ + distance₂)
2
Smart Bookkeeping
Keep a list of the best previous viewpoints that could pair with future ones
3
Sliding Window
As you move along the route, remove viewpoints that are now too far away
4
Maintain the Best
Only keep viewpoints that could potentially give the maximum score
Key Takeaway
🎯 Key Insight: By mathematically transforming the equation and using a monotonic deque for sliding window maximum, we reduce the time complexity from O(n²) to O(n) while maintaining all valid pairs within the distance constraint.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Each point is added and removed from deque at most once, giving us linear time

n
2n
Linear Growth
Space Complexity
O(n)

Deque can store up to n points in worst case

n
2n
Linearithmic Space

Constraints

  • 2 ≤ points.length ≤ 105
  • points[i].length == 2
  • -108 ≤ points[i][0], points[i][1] ≤ 108
  • 0 ≤ k ≤ 2 × 108
  • Points are sorted by x-coordinate: points[i][0] < points[j][0] for i < j
  • At least one valid pair exists that satisfies the distance constraint
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
23.6K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen