Tech

Missing Number (CSES 1083)

Published
Published:
Table of Contents
CSES Problem Set — Chapters

Problem Link: Missing Number

Approach

The expected sum of 1n1 \ldots n is n(n+1)/2n(n+1)/2. Subtract the given sum to find the missing number.

Solution

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long n, s = 0, x; cin >> n;
    for (int i = 0; i < n - 1; i++) { cin >> x; s += x; }
    cout << n * (n + 1) / 2 - s;
}
Support this post Sponsor