Minimum index i such that all the elements from index i to given index are equal | Java implementation of the approach ; Function to return the minimum required index ; Start from arr [ pos - 1 ] ; All elements are equal from arr [ i + 1 ] to arr [ pos ] ; Driver code ; Function Call
class GFG { static int minIndex ( int arr [ ] , int n , int pos ) { int num = arr [ pos ] ; int i = pos - 1 ; while ( i >= 0 ) { if ( arr [ i ] != num ) break ; i -- ; } return i + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 1 , 5 , 2 } ; int n = arr . length ; int pos = 4 ; System . out . println ( minIndex ( arr , n , pos ) ) ; } }