Find Missing Number in Sequence
Given an unsorted array of unique integers from 1 to n, one number is missing. Your task is to find and return the missing number.
[ 3, 7, 1, 2, 8, 4, 5 ]
Explanation. The array contains integers from 1 to 8 except for number 6.
[ 1, 2, 4 ]
Explanation. The array contains integers 1, 2, and 4. The missing number is 3.
[ 2, 3 ]
Explanation. The array misses number 1 for range 1 to 3.
[ 1, 2, 3, 4, 6, 7, 8, 9 ]
Explanation. The missing number between 1 and 9 in this array is 5.
[ 13, 11, 12, 15, 14, 10, 8, 9, 7, 6, 5, 4, 3, 2, 1 ]
Explanation. Given elements from 1 to 15 in unsorted order and the missing number in sequence is 16.
Follow-up: How would your solution change if the array can also have duplicate numbers?
The input array is guaranteed to have exactly one missing number and no duplicates. n is at least 2 and at most 1000.
- Views
- 4