The Wayback Machine - https://web.archive.org/web/20211222184817/https://www.geeksforgeeks.org/minimum-operations-to-convert-an-array-into-a-permutation-of-1-to-n-by-replacing-with-remainder-from-some-d/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimum operations to convert an Array into a Permutation of 1 to N by replacing with remainder from some d

  • Last Updated : 21 Dec, 2021

Given an array arr[] of size N, the task is to find the minimum number of operations to convert the array into a permutation of [1, n], in each operation, an element a[i] can be replaced by a[i] % d where d can be different in each operation performed. If it is not possible print -1.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {5, 4, 10, 8, 1}
Output: 2 
Explanation: In first operation choosing d = 7  , 10 can be replaced by 10 % 7  ,
In second operation d = 6, 8 can be replaced by 8 %6 so two operations.



Input : arr[] = {1, 2, 3, 7}
Output: -1

 

Approach: The task can be solved using the greedy approach. This approach is based on the fact that when remainder r is to be obtained, then a[i] > 2*r  i.e  r lies between the range [0, a[i]-1 /2]  

Let us take an example: 8 for different d

Taking, 8 % 7= 1

           8%6 = 2

           8%5 = 3

          8%4 = 0

          8%3 = 2



          8%2 = 0

          8%1=0

So maximum number that can be obtained is 3 using mod operation, so when we want to obtain a number i in a permutation then the number should a[i] > 2* i+1

Follow these steps to solve this problem:

  • Initialize a set s 
  • Traverse through the array arr[] & insert all the elements of arr[] into the set.
  • Initialize a variable ops = 0
  • Now iterate from n to 1
  • Check if s has i already if it has removed it from the set.
  • Else increment ops  and check if the largest element of the set < 2* i +1
  • If the largest of the set is < 2* i +1 then set ops = -1 and break out of the loop.
  • Else erase it from the set because we can make it i using mod operation.
  • Print the ops

`Below is the implementation of the above approach:

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the minimum operations
// to convert the array into a permutation of [1, n]
void minimum_operations(int arr[], int n)
{
    // Initialize the set
    set<int> s;
  
    // Insert all the elements into the set
    for (int i = 0; i < n; i++) {
        s.insert(arr[i]);
    }
  
    // Initialize ops to count the operations
    int ops = 0;
  
    // Traverse from [n to 1]
    for (int i = n; i >= 1; i--) {
  
        // If we already have i in our
        // array erase it from the set
        if (s.find(i) != s.end()) {
            s.erase(s.find(i));
        }
  
        // count the ops because there is no element
        else {
            ops++;
  
            // Check the largest element of the set
            auto it = s.end();
            it--;
  
            // If it is < 2*i +1 we cant get that i
            // using % operation so there is no way to
            // create a permutation
            if (*it < 2 * i + 1) {
                ops = -1;
                break;
            }
  
            // Erase it if we have processed
            // it to i by % operation
            s.erase(it);
        }
    }
  
    // Print the result
    cout << ops << endl;
}
  
// Driver Code
int main()
{
    // Initialize the value n
    int arr[] = { 5, 4, 10, 8, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    minimum_operations(arr, n);
  
    return 0;
}
Output
2

Time Complexity: O(nlogn)
Auxiliary Space: O(n)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!