Max Value of Equation - Problem
You are given an array points containing the coordinates of points on a 2D plane, sorted by the x-values, where points[i] = [xi, yi] such that xi < xj for all 1 <= i < j <= points.length. You are also given an integer k.
Return the maximum value of the equation yi + yj + |xi - xj| where |xi - xj| <= k and 1 <= i < j <= points.length.
It is guaranteed that there exists at least one pair of points that satisfy the constraint |xi - xj| <= k.
Input & Output
Example 1 — Basic Case
$
Input:
points = [[1,3],[2,0],[5,10],[6,-10]], k = 1
›
Output:
4
💡 Note:
The first two points satisfy |1 - 2| <= 1. The equation value is 3 + 0 + |1 - 2| = 4.
Example 2 — Multiple Valid Pairs
$
Input:
points = [[0,0],[3,0],[9,2]], k = 3
›
Output:
3
💡 Note:
Points [0,0] and [3,0] satisfy |0 - 3| <= 3. The equation value is 0 + 0 + |0 - 3| = 3.
Example 3 — Larger Distance
$
Input:
points = [[-17,-15],[-10,3],[2,15]], k = 12
›
Output:
16
💡 Note:
Points [-10,3] and [2,15] satisfy |(-10) - 2| <= 12. The equation value is 3 + 15 + |(-10) - 2| = 30. But points [-17,-15] and [-10,3] give (-15) + 3 + |(-17) - (-10)| = 16.
Constraints
- 2 ≤ points.length ≤ 105
- points[i].length == 2
- -108 ≤ points[i][0], points[i][1] ≤ 108
- 0 ≤ k ≤ 2 × 108
- points[i][0] < points[j][0] for all 1 ≤ i < j ≤ points.length
Visualization
Tap to expand
Understanding the Visualization
1
Input
Points sorted by x-coordinate and distance limit k
2
Find Pairs
Identify all valid pairs where |xi - xj| ≤ k
3
Calculate Maximum
Compute yi + yj + |xi - xj| and return maximum
Key Takeaway
🎯 Key Insight: Transform equation to (yi - xi) + (yj + xj) and use monotonic deque for O(n) solution
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code