Created
May 30, 2020 01:27
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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