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.
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.
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- 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 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 the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find missing number in another array which is shuffled copy
- Minimum cost to make an Array a permutation of first N natural numbers
- Find the smallest positive number missing from an unsorted array | Set 3
- Find the smallest positive number missing from an unsorted array | Set 2
- Find the smallest positive number missing from an unsorted array | Set 1
- Find the missing number in a sorted array of limited range
- Find the smallest positive number missing from an unsorted array : Hashing Implementation
- Find ratio of zeroes, positive numbers and negative numbers in the Array
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Find the Missing Number
- Find k numbers with most occurrences in the given array
- Find the longest sub-array having exactly k odd numbers