Minimum operations required to make all the array elements equal | C ++ implementation of the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperation ( int n , int k , int a [ ] ) { for ( int i = k ; i < n ; i ++ ) { if ( a [ i ] != a [ k - 1 ] ) cout << ( -1 ) << endl ; } for ( int i = k - 2 ; i > -1 ; i -- ) { if ( a [ i ] != a [ k - 1 ] ) cout << ( i + 1 ) << endl ; } } int main ( ) { int n = 5 ; int k = 3 ; int a [ ] = { 2 , 1 , 1 , 1 , 1 } ; minOperation ( n , k , a ) ; }