Maximum the value of a given expression for any pair of coordinates on a 2D plane | C ++ program for the above approach ; Function to find the maximum value of the given expression possible for any pair of co - ordinates ; Stores the differences between pairs ; Stores the maximum value ; Traverse the array arr [ ] [ ] ; While pq is not empty and difference between point [ 0 ] and pq . top ( ) [ 1 ] > K ; Removes the top element ; If pq is not empty ; Update the value res ; Push pair { point [ 1 ] - point [ 0 ] , point [ 0 ] } in pq ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxValueOfEquation ( vector < vector < int > > & arr , int K ) { priority_queue < vector < int > > pq ; int res = INT_MIN ; for ( auto point : arr ) { while ( ! pq . empty ( ) && point [ 0 ] - pq . top ( ) [ 1 ] > K ) { pq . pop ( ) ; } if ( ! pq . empty ( ) ) { res = max ( res , pq . top ( ) [ 0 ] + point [ 0 ] + point [ 1 ] ) ; } pq . push ( { point [ 1 ] - point [ 0 ] , point [ 0 ] } ) ; } cout << res ; } int main ( ) { vector < vector < int > > arr = { { 1 , 3 } , { 2 , 0 } , { 5 , 10 } , { 6 , -10 } } ; int K = 1 ; findMaxValueOfEquation ( arr , K ) ; return 0 ; }