# Minimize value of equation (yi + yj + |xi – xj|) using given points

Given an array** arr[]** of size **N** where **arr[i]** is in the form **[x _{i}, y_{i}]** denoting a point

**(x**on a 2D plane. The array is sorted on x-coordinates. Also, an integer

_{i}, y_{i})**K**is given. The task is to

**minimize**the value of the equation

**y**where

_{i}+ y_{j}+ |x_{i}– x_{j}|**|x**and

_{i}– x_{j}| ≤ K**1 ≤ i < j ≤ N**. It is guaranteed that there exists at least one pair of points that satisfy the constraint

**|x**

_{i}– x_{j}| ≤ K.**Examples:**

Input:arr[4] = {{1, 3}, {2, 0}, {5, 10}, {6, -10}}, K = 1Output:1Explanation:The first two points satisfies the given condition |x_{i}– x_{j}| ≤ 1.

Now, the equation = 3 + 0 + |1 – 2| = 4.

Similarly, Third and fourth points also satisfy the condition and

give a value of 10 + -10 + |5 – 6| = 1.

Except these 2 pairs no other points satisfy the given condition.

Minimum of 4 and 1 is 1. So output is 1.

Input:arr[4] = {{0, 0}, {3, 0}, {9, 2}}, K = 3Output:3Explanation:Only the first two points have an absolute difference of 3.

It gives the value of 0 + 0 + |0 – 3| = 3.

**Approach: **The problem reduces to find the minimum value of **(y _{j} + x_{j}) + (y_{i} – x_{i})**. Since the absolute difference of the

**x**coordinates of the points considered must be less than or equal to

**K**so the problem can be solved using a minimum heap. For each and every point consider the top value of the heap and check if the

**x**coordinate of the top and the current point is less than

**K**, take minimum among those and update the heap and follow the same for the rest of the coordinates. Follow the steps below to solve this problem:

- Initialize the minimum priority queue
**minh[].** - Initialize the variable
**min_val**as**INT_MAX**. - Iterate over the range
**[0, n)**using the variable**index**and perform the following tasks:- Pop the elements from the min-heap
**minh[]**until the difference in**x-coordinates**is greater than**K**. - If the
**heap**is not empty, then update the value of**min_val.** - Push the value of
**{arr[index][1] – arr[index][0], arr[index][0]}.**

- Pop the elements from the min-heap
- After performing the above steps, print the value of
**min_val**as the answer.

Below is the implementation of the above approach.

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `typedef` `pair<` `int` `, ` `int` `> pi;` `// Utility function to find minimum` `// value of equation` `int` `MinimumValue(` `int` `arr[][2], ` `int` `n, ` `int` `k)` `{` ` ` `// Minimum heap for storing` ` ` `// (x-y) difference and x` ` ` `// coordinate as the pair` ` ` `priority_queue<pi, vector<pi>,` ` ` `greater<pi> >` ` ` `minh;` ` ` `int` `min_val = INT_MAX;` ` ` `for` `(` `int` `index = 0; index < n; index++) {` ` ` `// Pop until the x-coordinates` ` ` `// difference is greater than K` ` ` `while` `((` ` ` `!minh.empty()` ` ` `&& ((arr[index][0]` ` ` `- minh.top().second)` ` ` `> k))) {` ` ` `minh.pop();` ` ` `}` ` ` `// If not heap empty update min_val` ` ` `if` `(!minh.empty()) {` ` ` `min_val` ` ` `= min(min_val,` ` ` `arr[index][0]` ` ` `+ arr[index][1]` ` ` `+ minh.top().first);` ` ` `}` ` ` `// Push the current pair in the heap` ` ` `minh.push({ arr[index][1]` ` ` `- arr[index][0],` ` ` `arr[index][0] });` ` ` `}` ` ` `return` `min_val;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Input taken` ` ` `int` `arr[4][2]` ` ` `= { { 1, 3 }, { 2, 0 },` ` ` `{ 5, 10 }, { 6, -10 } };` ` ` `int` `K = 1;` ` ` `int` `n = 4;` ` ` `// Function called` ` ` `cout << MinimumValue(arr, n, K) << ` `"\n"` `;` ` ` `return` `0;` `}` |

## Python3

`# python3 program for the above approach` `from` `queue ` `import` `PriorityQueue` `INT_MAX ` `=` `2147483647` `# Utility function to find minimum` `# value of equation` `def` `MinimumValue(arr, n, k):` ` ` `# Minimum heap for storing` ` ` `# (x-y) difference and x` ` ` `# coordinate as the pair` ` ` `minh ` `=` `PriorityQueue()` ` ` `min_val ` `=` `INT_MAX` ` ` `for` `index ` `in` `range` `(` `0` `, n):` ` ` `# Pop until the x-coordinates` ` ` `# difference is greater than K` ` ` `while` `(` `not` `minh.empty()):` ` ` `pi ` `=` `minh.get()` ` ` `if` `((arr[index][` `0` `] ` `-` `(` `-` `1` `*` `pi[` `1` `])) <` `=` `k):` ` ` `minh.put(pi)` ` ` `break` ` ` `# If not heap empty update min_val` ` ` `if` `(` `not` `minh.empty()):` ` ` `pi ` `=` `minh.get()` ` ` `min_val ` `=` `min` `(min_val, arr[index][` `0` `] ` `+` `arr[index][` `1` `] ` `+` `(` `-` `1` `*` `pi[` `1` `]))` ` ` `minh.put(pi)` ` ` `# Push the current pair in the heap` ` ` `minh.put([` `-` `1` `*` `arr[index][` `1` `]` ` ` `-` `(` `-` `1` `*` `arr[index][` `0` `]),` ` ` `-` `1` `*` `arr[index][` `0` `]])` ` ` `return` `min_val` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `# Input taken` ` ` `arr ` `=` `[[` `1` `, ` `3` `], [` `2` `, ` `0` `],` ` ` `[` `5` `, ` `10` `], [` `6` `, ` `-` `10` `]]` ` ` `K ` `=` `1` ` ` `n ` `=` `4` ` ` `# Function called` ` ` `print` `(MinimumValue(arr, n, K))` ` ` `# This code is contributed by rakeshsahni` |

**Output**

1

**Time Complexity:** O(NlogN)**Auxiliary Space:** O(N)