Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | Java program for the above approach ; Function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; Stores the required result ; Traverse over all the pairs ( i , j ) ; Calculate the value of the expression and update the overall maximum value ; Print the result ; Driver Code
class GFG { static void maximumValue ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { ans = Math . max ( ans , arr [ i ] + arr [ j ] + i - j ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 9 , 3 , 6 , 5 } ; int N = arr . length ; maximumValue ( arr , N ) ; } }