Given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing in the list. Write an efficient code to find the missing integer.
Input : arr = [1, 2, 3, 4, 6, 7, 8] Output : 5 Input : arr = [1, 2, 3, 4, 5, 6, 8, 9] Output : 7
One Simple solution is to apply methods discussed for finding the missing element in an unsorted array. Time complexity of this solution is O(n).
An efficient solution is based on the divide and conquer algorithm that we have seen in binary search, the concept behind this solution is that the elements appearing before the missing element will have ar[i] – i = 1 and those appearing after the missing element will have ar[i] – i = 2.
This solution has a time complexity of O(log n)
Missing number: 7
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.
- Find the missing number in a sorted array of limited range
- Find the only missing number in a sorted array
- Find missing element in a sorted array of consecutive numbers
- k-th missing element in sorted array
- Find the smallest positive number missing from an unsorted array | Set 2
- Find four missing numbers in an array containing elements from 1 to N
- Find the missing number in Arithmetic Progression
- Find the missing number in Geometric Progression
- Find the one missing number in range
- Find the missing number in unordered Arithmetic Progression
- Find the smallest missing number
- Find the Missing Number
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Find the repeating and the missing | Added 3 new methods
- Find missing elements of a range
- Find Two Missing Numbers | Set 2 (XOR based solution)
- Find the missing digit x from the given expression
- Largest subset with M as smallest missing number
- Find the number of elements greater than k in a sorted array
- Print missing elements that lie in range 0 - 99
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.