Junwon Kim.

CSES 1083 - Missing Number

CSES 1083 - Missing Number

CSES Problem Set, Problem Solving

Problem & Approach

Given all numbers between 1 and n except one, find the missing number.

Approach

  • Key Observation
    • The sum of 1 to n is n * (n + 1) / 2.
    • Subtracting the sum of the given n-1 numbers gives the missing number.

Related Concepts

  1. Math
  2. Prefix Sum

Implementation with C++

#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long sum = n * (n + 1) / 2; for (int i = 0; i < n - 1; i++) { long long x; cin >> x; sum -= x; } cout << sum << "\n"; return 0; }

Contact.

© 2025 Junwon Kim. All Rights Reserved.