Missing number in array
Subscribe to my channel --- / @codersbyyutish3249
Link to Ultimate Leetcode Challenge Playlist --- • Ultimate Leetcode Challenge
Also checkout my playlist on Coding Interview Problems --- • Easiest Coding Interview Problem Ever.
Problem Statement:
Given an array of size N-1 such that it can only contain distinct integers in the range of 1 to N. Find the missing element.
Example 1:
Input:
N = 5
A[] = {1,2,3,5}
Output: 4
Example 2:
Input:
N = 10
A[] = {1,2,3,4,5,6,7,8,10}
Output: 9
Your Task :
Complete the function MissingNumber() that takes array and N as input and returns the value of the missing number.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 106
1 ≤ A[i] ≤ 106
Solution:
int MissingNumber(...) {
int res = accumulate(array.begin(),array.end(),0);
return n*(n+1)/2 - res;
}
#shorts #timeenthusiast