beginner · math · arrays · arithmetic series
The one ticket that never came back
Problem
Tickets numbered 0 to n were handed out. Exactly n of them came back, each once, in no particular order. Find the number of the missing ticket.
Constraints
- 0 ≤ n ≤ 32 (visualization limit)
- the returned numbers are distinct and between 0 and n
Examples
[3, 0, 1] → 2
[0] → 1
Show the solution
Expected sum minus actual sum
0 + 1 + … + n = n(n + 1)/2. Every returned ticket contributes its number once, so the difference is the missing one.
Why it is correct
The actual sum equals the expected sum minus the missing number, because the returned numbers are exactly {0, …, n} without it.
Complexity
Time O(n), extra space O(1). One pass to add the n values; the formula is O(1).
For contrast: Check each number 0…n with a linear search. That costs O(n²) time and O(1) space.
Edge cases
- Missing 0: the difference is 0.
- Missing n: the last number.
- n = 0 (empty list): the missing ticket is 0.
Common mistakes
- Overflow of n(n + 1)/2 for large n in 32-bit integers (use 64-bit or XOR instead).
- Using n = largest value seen instead of the list length.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class MissingNumber
{
/// <summary>The one number in 0..n missing from n distinct values. O(n), O(1) space.</summary>
public static int Find(int[] tickets)
{
int n = tickets.Length;
int expected = n * (n + 1) / 2; // 0 + 1 + ... + n
int actual = 0;
foreach (int ticket in tickets)
actual += ticket;
return expected - actual;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/MissingNumber.cs
from collections.abc import Sequence
def find(tickets: Sequence[int]) -> int:
"""The one number in 0..n missing from n distinct values. O(n), O(1) space."""
n = len(tickets)
expected = n * (n + 1) // 2 # 0 + 1 + ... + n
actual = 0
for ticket in tickets:
actual += ticket
return expected - actual
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/missing_number.py
package dev.bekadan.dsa.problems;
public final class MissingNumber {
private MissingNumber() {
}
/** The one number in 0..n missing from n distinct values. O(n), O(1) space. */
public static int find(int[] tickets) {
int n = tickets.length;
int expected = n * (n + 1) / 2; // 0 + 1 + ... + n
int actual = 0;
for (int ticket : tickets) {
actual += ticket;
}
return expected - actual;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/MissingNumber.java
#pragma once
#include <span>
namespace bekadan::dsa::problems {
// The one number in 0..n missing from n distinct values. O(n), O(1) space.
inline int missing_number(std::span<const int> tickets) {
const int n = static_cast<int>(tickets.size());
const int expected = n * (n + 1) / 2; // 0 + 1 + ... + n
int actual = 0;
for (int ticket : tickets)
actual += ticket;
return expected - actual;
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/missing_number.hpp