Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | Javascript program to for the above approach ; Function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; Stores the required result ; Traversing over all the pairs ( i , j ) ; Calculate the value of the expression and update the overall maximum value ; Print the result ; Driver code
var arr = [ 1 , 9 , 3 , 6 , 5 ] ; function maximumValue ( arr , n ) { var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { ans = Math . max ( ans , arr [ i ] + arr [ j ] + i - j ) ; } } document . write ( ans ) ; } n = arr . length maximumValue ( arr , n ) ;