# Find first k natural numbers missing in given array

Given an array of size n and a number k, we need to print first k natural numbers that are not there in given array.

Examples:

```Input : [2 3 4]
k = 3
Output : [1 5 6]

Input  : [-2 -3 4]
k = 2
Output : [1 2]
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

1) Sort the given array.
2) After sorting, we find position of first positive number in array.
3) Now we traverse the array and keep printing elements in gaps between two consecutive array elements.
4) If gaps don’t cover k missing numbers, we print numbers greater than the largest array element.

## C++

 `// C++ program to find missing k numbers ` `// in an array. ` `#include ` `using` `namespace` `std; ` ` `  `// Prints first k natural numbers in ` `// arr[0..n-1] ` `void` `printKMissing(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``sort(arr, arr + n); ` ` `  `    ``// Find first positive number ` `    ``int` `i = 0; ` `    ``while` `(i < n && arr[i] <= 0) ` `        ``i++; ` ` `  `    ``// Now find missing numbers ` `    ``// between array elements ` `    ``int` `count = 0, curr = 1; ` `    ``while` `(count < k && i < n) { ` `        ``if` `(arr[i] != curr) { ` `            ``cout << curr << ``" "``; ` `            ``count++; ` `        ``} ` `        ``else` `            ``i++; ` ` `  `        ``curr++; ` `    ``} ` ` `  `    ``// Find missing numbers after ` `    ``// maximum. ` `    ``while` `(count < k) { ` `        ``cout << curr << ``" "``; ` `        ``curr++; ` `        ``count++; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, 3, 4 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `k = 3; ` `    ``printKMissing(arr, n, k); ` `    ``return` `0; ` `}`

## Java

 `// Java program to find missing k numbers ` `// in an array. ` `import` `java.util.Arrays; ` ` `  `class` `GFG { ` `    ``// Prints first k natural numbers in ` `    ``// arr[0..n-1] ` `    ``static` `void` `printKMissing(``int``[] arr, ``int` `n, ``int` `k) ` `    ``{ ` `        ``Arrays.sort(arr); ` ` `  `        ``// Find first positive number ` `        ``int` `i = ``0``; ` `        ``while` `(i < n && arr[i] <= ``0``) ` `            ``i++; ` ` `  `        ``// Now find missing numbers ` `        ``// between array elements ` `        ``int` `count = ``0``, curr = ``1``; ` `        ``while` `(count < k && i < n) { ` `            ``if` `(arr[i] != curr) { ` `                ``System.out.print(curr + ``" "``); ` `                ``count++; ` `            ``} ` `            ``else` `                ``i++; ` `            ``curr++; ` `        ``} ` ` `  `        ``// Find missing numbers after ` `        ``// maximum. ` `        ``while` `(count < k) { ` `            ``System.out.print(curr + ``" "``); ` `            ``curr++; ` `            ``count++; ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int``[] arr = { ``2``, ``3``, ``4` `}; ` `        ``int` `n = arr.length; ` `        ``int` `k = ``3``; ` `        ``printKMissing(arr, n, k); ` `    ``} ` `} ` `/* This code is contributed by Mr. Somesh Awasthi */`

## Python3

 `# Python3 program to find missing  ` `# k numbers in an array.  ` ` `  `# Prints first k natural numbers  ` `# in arr[0..n-1]  ` `def` `printKMissing(arr, n, k) : ` `     `  `    ``arr.sort()  ` ` `  `    ``# Find first positive number  ` `    ``i ``=` `0` `    ``while` `(i < n ``and` `arr[i] <``=` `0``) : ` `        ``i ``=` `i ``+` `1` ` `  `    ``# Now find missing numbers  ` `    ``# between array elements  ` `    ``count ``=` `0` `    ``curr ``=` `1` `    ``while` `(count < k ``and` `i < n) :  ` `        ``if` `(arr[i] !``=` `curr) : ` `            ``print``(``str``(curr) ``+` `" "``, end ``=` `'') ` `            ``count ``=` `count ``+` `1` `        ``else``: ` `            ``i ``=` `i ``+` `1` `         `  `        ``curr ``=` `curr ``+` `1` `         `  `    ``# Find missing numbers after  ` `    ``# maximum.  ` `    ``while` `(count < k) : ` `        ``print``(``str``(curr) ``+` `" "``, end ``=` `'')  ` `        ``curr ``=` `curr ``+` `1` `        ``count ``=` `count ``+` `1` `         `  `# Driver code  ` `arr ``=` `[ ``2``, ``3``, ``4` `]  ` `n ``=` `len``(arr)  ` `k ``=` `3` `printKMissing(arr, n, k);  ` ` `  `# This code is contributed  ` `# by Yatin Gupta `

## C#

 `// C# program to find missing  ` `// k numbers in an array. ` `using` `System; ` ` `  `class` `GFG { ` `    ``// Prints first k natural numbers ` `    ``// in arr[0..n-1] ` `    ``static` `void` `printKMissing(``int``[] arr,  ` `                              ``int` `n,  ` `                              ``int` `k) ` `    ``{ ` `        ``Array.Sort(arr); ` ` `  `        ``// Find first positive number ` `        ``int` `i = 0; ` `        ``while` `(i < n && arr[i] <= 0) ` `            ``i++; ` ` `  `        ``// Now find missing numbers ` `        ``// between array elements ` `        ``int` `count = 0, curr = 1; ` `        ``while` `(count < k && i < n) { ` `            ``if` `(arr[i] != curr) { ` `                ``Console.Write(curr + ``" "``); ` `                ``count++; ` `            ``} ` `            ``else` `                ``i++; ` `            ``curr++; ` `        ``} ` ` `  `        ``// Find missing numbers  ` `        ``// after maximum. ` `        ``while` `(count < k) { ` `            ``Console.Write(curr + ``" "``); ` `            ``curr++; ` `            ``count++; ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = {2, 3, 4}; ` `        ``int` `n = arr.Length; ` `        ``int` `k = 3; ` `        ``printKMissing(arr, n, k); ` `    ``} ` `} ` ` `  `// This code is contributed by Nitin Mittal `

## PHP

 ` `

Output:

```1 5 6
```

Time Complexity: O(n Log n)

This article is contributed by Biswajit Mohapatra. 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : nitin mittal, YatinGupta

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.