Given an array A of n elements. We need to change the array into a permutation of numbers from 1 to n using minimum replacements in the array.
Examples:
Input : A[] = {2, 2, 3, 3}
Output : 2 1 3 4
Explanation:
To make it a permutation of 1 to 4, 1 and 4 are
missing from the array. So replace 2, 3 with
1 and 4.
Input : A[] = {1, 3, 2}
Output : 1 3 2
Input : A[] = {10, 1, 2}
Output : 3 1 2
Approach:Observe that we don’t need to change the numbers which are in the range [1, n] and which are distinct(has only one occurrence). So, we use a greedy approach. If we meet the number we have never met before and this number is between 1 and n, we leave this number unchanged. And remove the duplicate elements and add the missing elements in the range [1, n]. Also replace the numbers, not in the range.
C++
// CPP program to make a permutation of numbers // from 1 to n using minimum changes. #include <bits/stdc++.h> using namespace std; void makePermutation(int a[], int n) { // Store counts of all elements. unordered_map<int, int> count; for (int i = 0; i < n; i++) count[a[i]]++; int next_missing = 1; for (int i = 0; i < n; i++) { if (count[a[i]] != 1 || a[i] > n || a[i] < 1) { count[a[i]]--; // Find next missing element to put // in place of current element. while (count.find(next_missing) != count.end()) next_missing++; // Replace with next missing and insert the // missing element in hash. a[i] = next_missing; count[next_missing] = 1; } } } // Driver Code int main() { int A[] = { 2, 2, 3, 3 }; int n = sizeof(A) / sizeof(A[0]); makePermutation(A, n); for (int i = 0; i < n; i++) cout << A[i] << " "; return 0; } |
Java
// Java program to make a permutation of numbers // from 1 to n using minimum changes. import java.util.*; class GFG { static void makePermutation(int []a, int n) { // Store counts of all elements. HashMap<Integer, Integer> count = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { if(count.containsKey(a[i])) { count.put(a[i], count.get(a[i]) + 1); } else { count.put(a[i], 1); } } int next_missing = 1; for (int i = 0; i < n; i++) { if (count.containsKey(a[i]) && count.get(a[i]) != 1 || a[i] > n || a[i] < 1) { count.put(a[i], count.get(a[i]) - 1); // Find next missing element to put // in place of current element. while (count.containsKey(next_missing)) next_missing++; // Replace with next missing and insert // the missing element in hash. a[i] = next_missing; count. put(next_missing, 1); } } } // Driver Code public static void main(String[] args) { int A[] = { 2, 2, 3, 3 }; int n = A.length; makePermutation(A, n); for (int i = 0; i < n; i++) System.out.print(A[i] + " "); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 code to make a permutation # of numbers from 1 to n using # minimum changes. def makePermutation (a, n): # Store counts of all elements. count = dict() for i in range(n): if count.get(a[i]): count[a[i]] += 1 else: count[a[i]] = 1; next_missing = 1 for i in range(n): if count[a[i]] != 1 or a[i] > n or a[i] < 1: count[a[i]] -= 1 # Find next missing element to put # in place of current element. while count.get(next_missing): next_missing+=1 # Replace with next missing and # insert the missing element in hash. a[i] = next_missing count[next_missing] = 1 # Driver Code A = [ 2, 2, 3, 3 ] n = len(A) makePermutation(A, n) for i in range(n): print(A[i], end = " ") # This code is contributed by "Sharad_Bhardwaj". |
C#
// C# program to make a permutation of numbers // from 1 to n using minimum changes. using System; using System.Collections.Generic; class GFG { static void makePermutation(int []a, int n) { // Store counts of all elements. Dictionary<int, int> count = new Dictionary<int, int>(); for (int i = 0; i < n; i++) { if(count.ContainsKey(a[i])) { count[a[i]] = count[a[i]] + 1; } else { count.Add(a[i], 1); } } int next_missing = 1; for (int i = 0; i < n; i++) { if (count.ContainsKey(a[i]) && count[a[i]] != 1 || a[i] > n || a[i] < 1) { count[a[i]] = count[a[i]] - 1; // Find next missing element to put // in place of current element. while (count.ContainsKey(next_missing)) next_missing++; // Replace with next missing and insert // the missing element in hash. a[i] = next_missing; count.Add(next_missing, 1); } } } // Driver Code public static void Main(String[] args) { int []A = { 2, 2, 3, 3 }; int n = A.Length; makePermutation(A, n); for (int i = 0; i < n; i++) Console.Write(A[i] + " "); } } // This code is contributed by Princi Singh |
Output:
1 2 4 3
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.
Recommended Posts:
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum steps to convert an Array into permutation of numbers from 1 to N
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Check if an Array is a permutation of numbers from 1 to N : Set 2
- Minimum cost to make an Array a permutation of first N natural numbers
- Check if an Array is a permutation of numbers from 1 to N
- Count array elements that can be maximized by adding any permutation of first N natural numbers
- Permutation of an array that has smaller values from another array
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Find permutation array from the cumulative sum array
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Sort permutation of N natural numbers using triple cyclic right swaps
- Count of subarrays of size K which is a permutation of numbers from 1 to K
- Number of ways to change the Array such that largest element is LCM of array
- Find the permutation p from the array q such that q[i] = p[i+1] - p[i]
- Lexicographically largest permutation of the array such that a[i] = a[i-1] + gcd(a[i-1], a[i-2])
- Restore a permutation from the given helper array
- Longest permutation subsequence in a given array
- Find index after traversing a permutation Array of 1 to N by K steps
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

