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 ; Prthe result ; Driver Code
def findMaxValueOfEquation ( arr , K ) : NEW_LINE INDENT pq = [ ] NEW_LINE res = - 10 ** 8 NEW_LINE for point in arr : NEW_LINE INDENT while ( len ( pq ) > 0 and point [ 0 ] - pq [ - 1 ] [ 1 ] > K ) : NEW_LINE INDENT del pq [ - 1 ] NEW_LINE DEDENT if ( len ( pq ) > 0 ) : NEW_LINE INDENT res = max ( res , pq [ - 1 ] [ 0 ] + point [ 0 ] + point [ 1 ] ) NEW_LINE DEDENT pq . append ( [ point [ 1 ] - point [ 0 ] , point [ 0 ] ] ) NEW_LINE pq = sorted ( pq ) NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 3 ] , [ 2 , 0 ] , [ 5 , 10 ] , [ 6 , - 10 ] ] NEW_LINE K = 1 NEW_LINE findMaxValueOfEquation ( arr , K ) NEW_LINE DEDENT