Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | C ++ program for the above approach ; Function to find the maximum value of ( arr [ i ] + arr [ j ] + i - j ) possible for a pair in the array ; Stores the maximum value of ( arr [ i ] + i ) ; Stores the required result ; Traverse the array arr [ ] ; Calculate for current pair and update maximum value ; Update maxValue if ( arr [ i ] + I ) is greater than maxValue ; Print the result ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumValue ( int arr [ ] , int n ) { int maxvalue = arr [ 0 ] ; int result = 0 ; for ( int i = 1 ; i < n ; i ++ ) { result = max ( result , maxvalue + arr [ i ] - i ) ; maxvalue = max ( maxvalue , arr [ i ] + i ) ; } cout << result ; } int main ( ) { int arr [ ] = { 1 , 9 , 3 , 6 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumValue ( arr , N ) ; return 0 ; }