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++

`// C++ implementation of above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print array ` `void` `printArray(` `int` `[], ` `int` `); ` ` ` `// Function to fill the position ` `// with arr[i] = 0 ` `void` `solve(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `set<` `int` `> unfilled_indices; ` ` ` `set<` `int` `> missing; ` ` ` ` ` `// Inserting all elements in ` ` ` `// missing[] set from 1 to N ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `missing.insert(i); ` ` ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` ` ` `// Inserting unfilled positions ` ` ` `if` `(arr[i] == 0) ` ` ` `unfilled_indices.insert(i); ` ` ` ` ` `// Removing allocated_elements ` ` ` `else` `{ ` ` ` `auto` `it = missing.find(arr[i]); ` ` ` `missing.erase(it); ` ` ` `} ` ` ` `} ` ` ` `auto` `it2 = missing.end(); ` ` ` `it2--; ` ` ` ` ` `// Loop for filling the positions ` ` ` `// with arr[i] != i ` ` ` `for` `(` `auto` `it = unfilled_indices.begin(); ` ` ` `it != unfilled_indices.end(); ` ` ` `it++, it2--) { ` ` ` `arr[*it] = *it2; ` ` ` `} ` ` ` `int` `pos = 0; ` ` ` ` ` `// Checking for any arr[i] = i ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` `if` `(arr[i] == i) { ` ` ` `pos = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `int` `x; ` ` ` ` ` `// Finding the suitable position ` ` ` `// in the array to swap with found i ` ` ` `// for which arr[i] = i ` ` ` `if` `(pos != 0) { ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` `if` `(pos != i) { ` ` ` ` ` `// Checking if the position ` ` ` `// is present in unfilled_position ` ` ` `if` `(unfilled_indices.find(i) ` ` ` `!= unfilled_indices.end()) { ` ` ` ` ` `// Swapping arr[i] & arr[pos] ` ` ` `// (arr[pos] = pos) ` ` ` `x = arr[i]; ` ` ` `arr[i] = pos; ` ` ` `arr[pos] = x; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `printArray(arr, n); ` `} ` ` ` `// Function to Print the array ` `void` `printArray(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `cout << arr[i] << ` `" "` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 0, 7, 4, 0, ` ` ` `3, 0, 5, 1 }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `solve(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

7 4 6 3 2 5 1

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:

- Difference Between Flood-fill and Boundary-fill Algorithm
- Find first k natural numbers missing in given array
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Partition first N natural number into two sets such that their sum is not coprime
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- k-th missing element in increasing sequence which is not present in a given sequence
- Reorder an array such that sum of left half is not equal to sum of right half
- Count natural numbers whose factorials are divisible by x but not y
- Sum of first N natural numbers which are not powers of K
- Count pairs of natural numbers with GCD equal to given number
- Divide first N natural numbers into 3 equal sum subsets
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find the permutation of first N natural numbers such that sum of i % P
_{i}is maximum possible - Fill an array based on frequency where elements are in range from 0 to n-1
- Fill array with 1's using minimum iterations of filling neighbors
- Find four missing numbers in an array containing elements from 1 to N
- Find missing element in a sorted array of consecutive numbers
- Count of Missing Numbers in a sorted array
- Find all missing numbers from a given sorted array

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.