Max Value of Equation

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.

Example 1
Inputpoints = [[1,3],[2,0],[5,10],[6,-10]], k = 1
Output4
The first two points give 4, the third and fourth points give 1, and no other pairs satisfy the condition, so the maximum is 4.
Example 2
Inputpoints = [[0,0],[3,0],[9,2]], k = 3
Output3
Only the first two points have an absolute difference of 3 or less in x-values, giving 0 + 0 + |0 - 3| = 3.

Constraints

  • 2 <= points.length <= 10^5
  • points[i].length == 2
  • -10^8 <= xi, yi <= 10^8
  • 0 <= k <= 2 * 10^8
  • xi < xj for all 1 <= i < j <= points.length
  • xi form a strictly increasing sequence.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate