Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element.
Explanation :
Calculate the expected sum of all numbers from 1 to n using the formula:
expected_sum = n * (n + 1) / 2
.Iterate through the array and calculate the actual sum of all the numbers.
missing number =actual sum - expected sum
// function to find missing number in an array // n is range of integer from 1 to n int missingNumber(vector<int>& array, int n) { int actual_sum = n*(n+1)/2 ,arr_sum=0, miss_no; for(int i=0 ; i<n-1 ; i++){ arr_sum += array[i]; } miss_no = actual_sum - arr_sum; return miss_no; }