Minimum index i such that all the elements from index i to given index are equal | 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
function minIndex ( arr , n , pos ) { var num = arr [ pos ] ; var i = pos - 1 ; while ( i >= 0 ) { if ( arr [ i ] != num ) break ; i -- ; } return i + 1 ; } var arr = [ 2 , 1 , 1 , 1 , 5 , 2 ] ; var n = arr . length ; var pos = 4 ; document . write ( minIndex ( arr , n , pos ) ) ;