> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/hashmap-and-hashset-and-sliding-window/untitled-10.md).

# Max Value of Equation

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`.

Find 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:**

```
Input: points = [[1,3],[2,0],[5,10],[6,-10]], k = 1
Output: 4
Explanation: The first two points satisfy the condition |xi - xj| <= 1 and if we calculate the equation we get 3 + 0 + |1 - 2| = 4. Third and fourth points also satisfy the condition and give a value of 10 + -10 + |5 - 6| = 1.
No other pairs satisfy the condition, so we return the max of 4 and 1.
```

**Example 2:**

```
Input: points = [[0,0],[3,0],[9,2]], k = 3
Output: 3
Explanation: Only the first two points have an absolute difference of 3 or less in the x-values, and give the value of 0 + 0 + |0 - 3| = 3.
```

**Constraints:**

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

```java
class Solution {
    public int findMaxValueOfEquation(int[][] points, int k) {
        // Because xi < xj,
        // yi + yj + |xi - xj| = (yi - xi) + (yj + xj)
        // So we only need to find out the maximum yi - xi.
        Deque<int[]> dq = new LinkedList<>();
        // Maintaing an increasing deque from right to left
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < points.length; i++) {
            // Kicking out of range k points
            while (dq.size() != 0 && dq.peekFirst()[0] + k < points[i][0])
                dq.pollFirst();
            if (dq.size() != 0)
                max = Math.max(max, dq.peekFirst()[1] + points[i][1] + (points[i][0] - dq.peekFirst()[0]));
            // Maintaining decreasing dq
            while (dq.size() != 0 && dq.peekLast()[1] - dq.peekLast()[0] < points[i][1] - points[i][0])
                dq.pollLast();
            dq.add(points[i]);
        }
        return max;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/hashmap-and-hashset-and-sliding-window/untitled-10.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
