Minimum number of operations required to make a permutation of first N natural numbers equal | C ++ program for the above approach ; Function to find the minimum number of operations required to make all array elements equal ; Store the count of operations required ; Increment by K - 1 , as the last element will be used again for the next K consecutive elements ; Increment count by 1 ; Return the result ; Driver Code ; Given Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumOperations ( int A [ ] , int N , int K ) { int Count = 0 ; int i = 0 ; while ( i < N - 1 ) { i = i + K - 1 ; Count ++ ; } return Count ; } int main ( ) { int A [ ] = { 5 , 4 , 3 , 1 , 2 } ; int K = 3 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << MinimumOperations ( A , N , K ) << endl ; return 0 ; }