Maximum Fixed Point ( Value equal to index ) in a given Array | Java 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
import java . io . * ; class GFG { static void findLargestIndex ( int arr [ ] , int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == i ) { System . out . println ( i ) ; return ; } } System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 10 , - 5 , 0 , 3 , 7 } ; int n = arr . length ; findLargestIndex ( arr , n ) ; } }