College Library -Editorial || COLIB

PROBLEM LINK: College Library | CodeChef

Problem Code: College Library | CodeChef

Practice: CodeChef | Competitive Programming | Participate & Learn | CodeChef

Contest : Code Blooded 2.0 Coding Competition | CodeChef

Author: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9
Tester: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9
Editorialist: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9

DIFFICULTY:

Easy

PROBLEM:

In your college library, there are N Volumes of Novel. You are given all volume numbers between 1,2,…,n except one. Your task is to find the missing volume number.

EXPLANATION:

By knowing this property of the natural numbers a simple solution emerges. Since we know there is only one number missing in the permutation [1, N] then we can subtract the sum of the input from the sum of all the natural numbers up to N, by doing this every number will cancel out except one, the missing number.

SOLUTION:

C++:

#include <bits/stdc++.h>
using namespace std;

int main() {
long long int n, input, sum = 0;
cin >> n;
for (int i = 0 ; i < n - 1 ; i++) {
cin >> input;
sum += input;
}
cout << ( n * (n + 1) ) / 2 - sum << endl;
return 0;
}