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.
Input : [2 3 4] k = 3 Output : [1 5 6] Input : [-2 -3 4] k = 2 Output : [1 2]
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.
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 email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find four missing numbers in an array containing elements from 1 to N
- Find missing element in a sorted array of consecutive numbers
- Find Two Missing Numbers | Set 1 (An Interesting Linear Time Solution)
- Find missing elements from an Array
- Find the missing integer in an array if mean is given
- Find missing number in another array which is shuffled copy
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find the smallest positive number missing from an unsorted array | Set 2
- Find the smallest positive number missing from an unsorted array | Set 3
- Find the missing number in a sorted array of limited range
- Find the smallest positive number missing from an unsorted array | Set 1
- Find the smallest positive number missing from an unsorted array : Hashing Implementation
- Find k numbers with most occurrences in the given array
- Find the longest sub-array having exactly k odd numbers
- Program for weighted mean of natural numbers.