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 firstname.lastname@example.org. 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 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 1
- Find the smallest positive number missing from an unsorted array | Set 2
- Find the missing number in a sorted array of limited range
- Find the smallest positive number missing from an unsorted array | Set 3
- Find the smallest positive number missing from an unsorted array : Hashing Implementation
- Find the longest sub-array having exactly k odd numbers
- Find k numbers with most occurrences in the given array
- Program for weighted mean of natural numbers.
- Find the Missing Number