Open In App

Minimum number of operations required to make a permutation of first N natural numbers equal

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array A[] of size N, containing a permutation of first N natural numbers and an integer K, the task is to find the minimum number of operations required to make all array elements equal by selecting K (1 < K ≤ N) consecutive array elements and replacing them with the minimum of the selected elements.

Examples:

Input: A[] = {4, 2, 1, 5, 3}, K = 3, N = 5
Output: 2
Explanation: Select consecutive elements from index 1 to 3 and replace with the minimum of {4, 2, 1}. Therefore, A[] becomes {1, 1, 1, 5, 3}.
Select consecutive elements from index from 3 to 5 and replace with minimum of {1, 5, 3}. Therefore, A becomes {1, 1, 1, 1, 1}.
Therefore, the total number of operations required are 2.

Input: A[] = {3, 6, 2, 1, 4, 5}, K = 2, N=6
Output: 5
Explanation: Select consecutive elements from index 4 to 5 and replace with the minimum of {1, 4} is 1. Therefore, A[] becomes {3, 6, 2, 1, 1, 5}
Select consecutive elements from index 5 to 6 and replace with the minimum of {1, 5} is 1. Therefore, A[] becomes {3, 6, 2, 1, 1, 1}
Select consecutive elements from index 3 to 4 and replace with the minimum of {2, 1} is 1. Therefore, A[] becomes {3, 6, 1, 1, 1, 1}
Select consecutive elements from index 2 to 3 and replace with the minimum of {6, 1} is 1. Therefore, A[] becomes {3, 1, 1, 1, 1, 1}
Select consecutive elements from index 1 to 2 and replace with the minimum of {3, 1} is 1. Therefore, A[] becomes {1, 1, 1, 1, 1, 1}
Therefore, the total number of operations are 5.

 

Approach: The idea is based on the following observations:

  • The permutation does not matter. It is the same as placing the minimum at the beginning.
  • The optimal number of operations required can be calculated by starting with the minimum index and moving forward by K.

The problem can be solved by imagining that the minimum is at the beginning of the array and from there onwards, selecting K consecutive elements. Follow the steps below to solve the problem:

  • Initialize the variables i and count as 0, for iterating and counting the minimum number of operations respectively.
  • Loop while i is less than N – 1 because when i reaches N – 1, all elements have been made equal. In each current iteration, perform the following steps:
    • Increment count by 1.
    • Increment i by K-1 because the rightmost updated element would be used again for the next segment.
  • Print the value of count as the result.

Below is the implementation of the above approach:

C++




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


Java




// Java program for the above approach
 
import java.io.*;
 
class GFG {
       
      // Function to find the minimum
    // number of operations required
    // to make all array elements equal
 
    static int MinimumOperations(int[] A, int N, int K)
    {
        // Store the count of
        // operations required
        int Count = 0;
 
        int i = 0;
 
        while (i < N - 1) {
 
            // Increment by K - 1, as the last
            // element will be used again for
            // the next K consecutive elements
            i = i + K - 1;
 
            // Increment count by 1
            Count++;
        }
 
        // Return the result
        return Count;
    }
 
    // Driver Code
    public static void main (String[] args) {
        // Given Input
        int[] A = { 5, 4, 3, 1, 2 };
        int K = 3;
        int N = A.length;
 
        System.out.println(MinimumOperations(A, N, K));
    }
}
 
// This code is contributed by Dharanendra L V.


Python3




# Python3 program for the above approach
 
# Function to find the minimum
# number of operations required
# to make all array elements equal
def MinimumOperations(A, N, K):
     
    # Store the count of
    # operations required
    Count = 0
 
    i = 0
 
    while (i < N - 1):
 
        # Increment by K - 1, as the last
        # element will be used again for
        # the next K consecutive elements
        i = i + K - 1
 
        # Increment count by 1
        Count += 1
 
    # Return the result
    return Count
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    A = [ 5, 4, 3, 1, 2 ]
    K = 3
    N = len(A)
 
    print (MinimumOperations(A, N, K))
 
# This code is contributed by mohit kumar 29


C#




// C# program for the above approach
using System;
class GFG {
    // Function to find the minimum
    // number of operations required
    // to make all array elements equal
 
    static int MinimumOperations(int[] A, int N, int K)
    {
        // Store the count of
        // operations required
        int Count = 0;
 
        int i = 0;
 
        while (i < N - 1) {
 
            // Increment by K - 1, as the last
            // element will be used again for
            // the next K consecutive elements
            i = i + K - 1;
 
            // Increment count by 1
            Count++;
        }
 
        // Return the result
        return Count;
    }
 
    // Driver Code
    public static void Main()
    {
        // Given Input
        int[] A = { 5, 4, 3, 1, 2 };
        int K = 3;
        int N = A.Length;
 
        Console.WriteLine(MinimumOperations(A, N, K));
    }
}
 
// This code is contributed by ukasp.


Javascript




<script>
 
        // JavaScript program for the above approach
 
        // Function to find the minimum
        // number of operations required
        // to make all array elements equal
 
        function MinimumOperations(A, N, K) {
            // Store the count of
            // operations required
            let Count = 0;
 
            let i = 0;
 
            while (i < N - 1) {
 
                // Increment by K - 1, as the last
                // element will be used again for
                // the next K consecutive elements
                i = i + K - 1;
 
                // Increment count by 1
                Count++;
            }
 
            // Return the result
            return Count;
        }
 
        // Driver Code
        // Given Input
        let A = [5, 4, 3, 1, 2];
        let K = 3;
        let N = A.length;
 
        document.write(MinimumOperations(A, N, K));
 
        // This code is contributed by Hritik
         
</script>


Output: 

2

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Last Updated : 04 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads