Change the array into a permutation of numbers from 1 to n
Last Updated :
28 Jul, 2022
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void makePermutation( int a[], int n)
{
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]]--;
while (count.find(next_missing) != count.end())
next_missing++;
a[i] = next_missing;
count[next_missing] = 1;
}
}
}
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
import java.util.*;
class GFG
{
static void makePermutation( int []a, int n)
{
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 );
while (count.containsKey(next_missing))
next_missing++;
a[i] = next_missing;
count. put(next_missing, 1 );
}
}
}
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] + " " );
}
}
|
Python3
def makePermutation (a, n):
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
while count.get(next_missing):
next_missing + = 1
a[i] = next_missing
count[next_missing] = 1
A = [ 2 , 2 , 3 , 3 ]
n = len (A)
makePermutation(A, n)
for i in range (n):
print (A[i], end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void makePermutation( int []a, int n)
{
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;
while (count.ContainsKey(next_missing))
next_missing++;
a[i] = next_missing;
count.Add(next_missing, 1);
}
}
}
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] + " " );
}
}
|
Javascript
<script>
function makePermutation(a, n)
{
var count = new Map();
for ( var i = 0; i < n; i++)
{
if (count.has(a[i]))
count.set(a[i], count.get(a[i])+1)
else
count.set(a[i], 1)
}
var next_missing = 1;
for ( var i = 0; i < n; i++) {
if (count.get(a[i]) != 1 || a[i] > n || a[i] < 1) {
count.set(a[i], count.get(a[i])-1);
while (count.has(next_missing))
next_missing++;
a[i] = next_missing;
count.set(next_missing,1);
}
}
}
var A = [2, 2, 3, 3];
var n = A.length;
makePermutation(A, n);
for ( var i = 0; i < n; i++)
document.write( A[i] + " " );
</script>
|
Time Complexity : O(n log n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...