Problem of the day
Given an array ‘a’ of size ‘n’-1 with elements of range 1 to ‘n’. The array does not contain any duplicates. Your task is to find the missing number.
Input:
'a' = [1, 2, 4, 5], 'n' = 5
Output :
3
Explanation: 3 is the missing value in the range 1 to 5.
First-line contains an integer ‘n’.
The second line has ‘n’-1 integers denoting the array ‘a’.
Output Format:-
You must return the only missing integer from 1 to ‘n’\
.
Note:-You don’t need to print anything. Just implement the given function.
4
1 2 3
4
4 is the missing value in the range 1 to 4.
8
1 2 3 5 6 7 8
4
4 is the missing value in the range 1 to 8.
The expected time complexity is O(n).
1 <= 'n' <= 10^6
1 <= 'a'[i] <= 'n'
Time Limit: 1 sec
A[i] ranges from 1 to N.
Approach:
Algorithm:
Function int missingNumber([int] A):
O( N ), Where ‘N’ is given input.
We are taking O( N ) time to compute the ‘hash’ array. We take O( N ) time to find the missing number. Hence, the overall complexity will be O( N ).
O( N ), Where ‘N’ is given input.
We are taking O( N ) extra space for maintaining the ‘hash’ array. Hence, the overall complexity will be O( N ).