Maximum Fixed Point ( Value equal to index ) in a given Array | C ++ implementation of the above approach ; Function to find the maximum index i such that arr [ i ] is equal to i ; Traversing the array from backwards ; If arr [ i ] is equal to i ; If there is no such index ; Driver code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findLargestIndex ( int arr [ ] , int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == i ) { cout << i << endl ; return ; } } cout << -1 << endl ; } int main ( ) { int arr [ ] = { -10 , -5 , 0 , 3 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findLargestIndex ( arr , n ) ; return 0 ; }