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.
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.
- 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
- Find Two Missing Numbers | Set 1 (An Interesting Linear Time Solution)
- Find the missing integer in an array if mean is given
- Find missing elements from an Array
- 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 smallest positive number missing from an unsorted array | Set 1
- Find the missing number in a sorted array of limited range
- Minimum cost to make an Array a permutation of first N natural numbers
- 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 the smallest missing number
- Find the missing elements from 1 to M in given N ranges
- Find n-variables from n sum equations with one missing