Maximum the value of a given expression for any pair of coordinates on a 2D plane | 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
function findMaxValueOfEquation ( arr , K ) { let pq = [ ] ; let res = Number . MIN_VALUE ; for ( let point = 0 ; point < arr . length ; point ++ ) { while ( pq . length != 0 && arr [ point ] [ 0 ] - pq [ 0 ] [ 1 ] > K ) { pq . shift ( ) ; } if ( pq . length != 0 ) { res = Math . max ( res , pq [ 0 ] [ 0 ] + arr [ point ] [ 0 ] + arr [ point ] [ 1 ] ) ; } pq . push ( [ arr [ point ] [ 1 ] - arr [ point ] [ 0 ] , arr [ point ] [ 0 ] ] ) ; pq . sort ( function ( a , b ) { if ( a [ 0 ] != b [ 0 ] ) return b [ 0 ] - a [ 0 ] ; return b [ 1 ] - a [ 1 ] ; } ) } document . write ( res ) ; } let arr = [ [ 1 , 3 ] , [ 2 , 0 ] , [ 5 , 10 ] , [ 6 , - 10 ] ] ; let K = 1 ; findMaxValueOfEquation ( arr , K ) ;