text: Given an array arr [ ] , find the maximum j | C program for the above approach ; For a given array arr [ ] , returns the maximum j a i such that arr [ j ] > arr [ i ] ; Driver program to test above functions
code: #include <stdio.h> NEW_LINE int maxIndexDiff ( int arr [ ] , int n ) { int maxDiff = -1 ; int i , j ; for ( i = 0 ; i < n ; ++ i ) { for ( j = n - 1 ; j > i ; -- j ) { if ( arr [ j ] > arr [ i ] && maxDiff < ( j - i ) ) maxDiff = j - i ; } } return maxDiff ; } int main ( ) { int arr [ ] = { 9 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 18 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int maxDiff = maxIndexDiff ( arr , n ) ; printf ( " % d " , maxDiff ) ; getchar ( ) ; return 0 ; }