Minimum operations required to make all the array elements equal | Function to return the minimum number of given operation required to make all the array elements equal ; Check if all the elements from kth index to last are equal ; Finding the 1 st element which is not equal to the kth element ; Driver code
function minOperation ( n , k , a ) { for ( i = k ; i < n ; i ++ ) { if ( a [ i ] != a [ k - 1 ] ) document . write ( - 1 ) ; } for ( i = k - 2 ; i > - 1 ; i -- ) { if ( a [ i ] != a [ k - 1 ] ) document . write ( i + 1 ) ; } } var n = 5 ; var k = 3 ; var a = [ 2 , 1 , 1 , 1 , 1 ] ; minOperation ( n , k , a ) ;