Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created May 30, 2020 01:27
Show Gist options
  • Select an option

  • Save Ch-sriram/ef37fc6877a6042f5c099edd27af5df2 to your computer and use it in GitHub Desktop.

Select an option

Save Ch-sriram/ef37fc6877a6042f5c099edd27af5df2 to your computer and use it in GitHub Desktop.
Given an array of size N, it contains all the numbers from 1 to N+1 inclusive, except one number. You have to find the missing number.
#include <iostream>
#include <vector>
using namespace std;
int findMissingNumber(int n) {
int sum = 0, x;
for(int i = 0; i < n; ++i, sum += x)
cin >> x;
int expected_sum = ((n+1)*(n+2)) / 2;
return expected_sum - sum;
}
int main() {
int t; cin >> t;
while(t--) {
int n; cin >> n;
cout << findMissingNumber(n) << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment