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
< ? php function minOperation ( $ n , $ k , & $ a ) { for ( $ i = $ k ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] != $ a [ $ k - 1 ] ) return -1 ; } for ( $ i = $ k - 2 ; $ i > -1 ; $ i -- ) { if ( $ a [ $ i ] != $ a [ $ k - 1 ] ) return ( $ i + 1 ) ; } } $ n = 5 ; $ k = 3 ; $ a = array ( 2 , 1 , 1 , 1 , 1 ) ; echo ( minOperation ( $ n , $ k , $ a ) ) ; ? >