Skip to main content

Missing Number

Task: Find the missing number from given an array of N numbers from 1 to N.

Example:
Input:
1
3
1 4 3
Output:
2

Here is the Code:
Program in C++:

  1. #include <iostream>
  2. using namespace std;

  3. int main() {
  4. //number of test cases
  5. int t;
  6. scanf("%d\n",&t);
  7. while(t--)
  8. {
  9.     int n;
  10.     scanf("%d\n",&n);
  11.     int a[n-1],sum=0;
  12.     for(int i=0;i<n-1;i++)
  13.     {
  14.         scanf("%d\n",&a[i]);
  15.         sum+=a[i];
  16.     }
  17.     int sn=(n*(n+1))/2;
  18.     cout<<sn-sum<<endl;
  19. }
  20. return 0;
  21. }

Here is the Video: