Given an unsorted array arr[] consisting of N natural numbers and the missing numbers as 0 such that arr[i] ? i, the task is to find and fill these missing numbers without changing the initial order. Please note that the array can contain numbers from 1 to N only once.
Examples:
Input: arr[] = {7, 4, 0, 3, 0, 5, 1}
Output: 7 4 6 3 2 5 1
Explanation:
In the given array, unfilled indices are 2 and 4. So the missing numbers that can be filled, to fulfil the criteria arr[i] ? i, are 6 and 2 respectively.
Input: arr[] = {2, 1, 0, 0, 0}
Output: 2 1 4 5 3
Explanation:
In the given array unfilled indices are 3, 4 and 5. So the missing numbers that can be filled, to fulfil the criteria arr[i] ? i, are 4, 5 and 3 respectively.
Approach:
- Store all the unfilled indices in an array unfilled_indices[]. i.e, for all i such that arr[i] = 0.
- Also store all the elements which are missing in the array missing[]. This can be done by first inserting all elements from 1 to N and then deleting all elements which is not 0
- Simply iterate the unfilled_indices[] array and start allocating all elements stored in missing[] array possibly taking each element from backwards(to avoid any i getting arr[i] = i).
- Now it may be possible that maximum one index can get the same arr[i] = i. Simply swap with it any other element after checking that the other index should be present in unfilled_indices[].
- Print the new array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printArray( int [], int );
void solve( int arr[], int n)
{
set< int > unfilled_indices;
set< int > missing;
for ( int i = 1; i < n; i++)
missing.insert(i);
for ( int i = 1; i < n; i++) {
if (arr[i] == 0)
unfilled_indices.insert(i);
else {
auto it = missing.find(arr[i]);
missing.erase(it);
}
}
auto it2 = missing.end();
it2--;
for ( auto it = unfilled_indices.begin();
it != unfilled_indices.end();
it++, it2--) {
arr[*it] = *it2;
}
int pos = 0;
for ( int i = 1; i < n; i++) {
if (arr[i] == i) {
pos = i;
}
}
int x;
if (pos != 0) {
for ( int i = 1; i < n; i++) {
if (pos != i) {
if (unfilled_indices.find(i)
!= unfilled_indices.end()) {
x = arr[i];
arr[i] = pos;
arr[pos] = x;
break ;
}
}
}
}
printArray(arr, n);
}
void printArray( int arr[], int n)
{
for ( int i = 1; i < n; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 0, 7, 4, 0,
3, 0, 5, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
solve(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void solve( int arr[], int n)
{
Set<Integer> unfilled_indices = new HashSet<Integer>();
Set<Integer> missing = new HashSet<Integer>();
for ( int i = 1 ; i < n; i++)
{
missing.add(i);
}
for ( int i = 1 ; i < n; i++)
{
if (arr[i] == 0 )
{
unfilled_indices.add(i);
}
else
{
missing.remove(arr[i]);
}
}
int [] mi = new int [missing.size()];
int l = 0 ;
for ( int x:missing)
{
mi[l++] = x;
}
int m = missing.size();
for ( int it:unfilled_indices)
{
arr[it] = mi[m - 1 ];
m--;
}
int pos = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (arr[i] == i)
{
pos = i;
}
}
int x;
if (pos != 0 )
{
for ( int i = 1 ; i < n; i++)
{
if (pos != i)
{
if (unfilled_indices.contains(i))
{
x = arr[i];
arr[i] = pos;
arr[pos] = x;
break ;
}
}
}
}
printArray(arr, n);
}
static void printArray( int arr[], int n)
{
for ( int i = 1 ; i < n; i++)
{
System.out.print(arr[i] + " " );
}
}
public static void main (String[] args)
{
int [] arr = { 0 , 7 , 4 , 0 , 3 , 0 , 5 , 1 };
int n = arr.length;
solve(arr, n);
}
}
|
Python3
def solve(arr, n):
unfilled_indices = {}
missing = {}
for i in range (n):
missing[i] = 1
for i in range ( 1 , n):
if (arr[i] = = 0 ):
unfilled_indices[i] = 1
else :
del missing[arr[i]]
it2 = list (missing.keys())
m = len (it2)
for it in unfilled_indices:
arr[it] = it2[m - 1 ]
m - = 1
pos = 0
for i in range ( 1 , n):
if (arr[i] = = i):
pos = i
x = 0
if (pos ! = 0 ):
for i in range ( 1 , n):
if (pos ! = i):
if i in unfilled_indices:
x = arr[i]
arr[i] = pos
arr[pos] = x
break
printArray(arr, n)
def printArray(arr, n):
for i in range ( 1 , n):
print (arr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 0 , 7 , 4 , 0 , 3 , 0 , 5 , 1 ]
n = len (arr)
solve(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void solve( int [] arr, int n)
{
HashSet< int > unfilled_indices = new HashSet< int >();
HashSet< int > missing = new HashSet< int >();
for ( int i = 1; i < n; i++)
{
missing.Add(i);
}
for ( int i = 1; i < n; i++)
{
if (arr[i] == 0)
{
unfilled_indices.Add(i);
}
else
{
missing.Remove(arr[i]);
}
}
int [] mi = new int [missing.Count];
int l = 0;
foreach ( int x in missing)
{
mi[l++] = x;
}
int m = missing.Count;
foreach ( int it in unfilled_indices)
{
arr[it] = mi[m - 1];
m--;
}
int pos = 0;
for ( int i = 1; i < n; i++)
{
if (arr[i] == i)
{
pos = i;
}
}
if (pos != 0)
{
for ( int i = 1; i < n; i++)
{
if (pos != i)
{
if (unfilled_indices.Contains(i))
{
int x = arr[i];
arr[i] = pos;
arr[pos] = x;
break ;
}
}
}
}
printArray(arr, n);
}
static void printArray( int [] arr, int n)
{
for ( int i = 1; i < n; i++)
{
Console.Write(arr[i] + " " );
}
}
static public void Main()
{
int [] arr = { 0, 7, 4, 0, 3, 0, 5, 1 };
int n = arr.Length;
solve(arr, n);
}
}
|
Javascript
<script>
function solve(arr,n)
{
let unfilled_indices = new Set();
let missing = new Set();
for (let i = 1; i < n; i++)
{
missing.add(i);
}
for (let i = 1; i < n; i++)
{
if (arr[i] == 0)
{
unfilled_indices.add(i);
}
else
{
missing. delete (arr[i]);
}
}
let mi = new Array(missing.size);
let l = 0;
for (let x of missing.values())
{
mi[l++] = x;
}
let m = missing.size;
for (let it of unfilled_indices.values())
{
arr[it] = mi[m - 1];
m--;
}
let pos = 0;
for (let i = 1; i < n; i++)
{
if (arr[i] == i)
{
pos = i;
}
}
let x;
if (pos != 0)
{
for (let i = 1; i < n; i++)
{
if (pos != i)
{
if (unfilled_indices.has(i))
{
x = arr[i];
arr[i] = pos;
arr[pos] = x;
break ;
}
}
}
}
printArray(arr, n);
}
function printArray(arr,n)
{
for (let i = 1; i < n; i++)
{
document.write(arr[i] + " " );
}
}
let arr=[0, 7, 4, 0,3, 0, 5, 1 ];
let n = arr.length;
solve(arr, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!