Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 18 Jan, 2022

Given an array arr[] of size N where arr[i] is in the form [xi, yi] denoting a point (xi, yi) on a 2D plane. The array is sorted on x-coordinates. Also, an integer K is given. The task is to minimize the value of the equation yi + yj + |xi – xj| where |xi – xj| ≤ K and 1 ≤ i < j ≤ N. It is guaranteed that there exists at least one pair of points that satisfy the constraint |xi – xj| ≤ K.

Examples:

Input: arr[4] = {{1, 3}, {2, 0}, {5, 10}, {6, -10}}, K = 1
Output: 1
Explanation: The first two points satisfies the given condition |xi – xj| ≤ 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 = 3
Output: 3
Explanation: 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 (yj + xj) + (yi – xi). 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]}.
  • 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)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!