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
- Dynamic Programming vs Divide-and-Conquer
- Cut all the rods with some length such that the sum of cut-off length is maximized
- First strictly smaller element in a sorted array in Java
- Modular exponentiation (Recursive)
- Find the only missing number in a sorted array
- Merge Sort with O(1) extra space merge and O(n lg n) time
- k-th missing element in sorted array
- Find position of an element in a sorted array of infinite numbers
- Binary Search
- Merge Sort
- Divide and Conquer Algorithm | Introduction
- Find the repeating and the missing | Added 3 new methods
- Search an element in a sorted and rotated array
- Find the Missing Number
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.