Sum it Up

Problem: you are given a sequence of numbers from 1 to n-1 with one of the numbers repeating only once. (example: 1 2 3 3 4 5). how can you find the repeating number? what if i give you the constraint that you can’t use a dynamic amount of memory (i.e. the amount of memory you use can’t be related to n)?
what if there are two repeating numbers (and the same memory constraint?)


Solution

as a programmer, my first answer to this problem would be make a bit vector of size n, and every time you see the number, set its correspond index bit to 1. if the bit is already set, then that’s the repeater. since there were no constraints in the question, this is an ok answer. its good because it makes sense if you draw it for someone, whether they are a programmer, mathemetician, or just your grandpa. its not the most efficient answer though.

now, if i add the constraint that you can only use a fixed amount of memory (i.e. not determined by n) and it must run in O(n) time… how do we solve it. adding all the numbers up from 1 to n-1 would give us a distinct sum. subtracting the total sum of all the numbers from the sum of n to n-1 ( which is (n)(n-1)/2 ) would give us the secret extra number.

what if you can only use a fixed amount of memory, and two of the numbers are repeated? we know that the numbers have a distinct sum, and the difference would be equal to the sum of our unknowns
c = a + b
where c is the sum and a and b are the unknowns – c is a constant
if we had another similar formula we could solve the two unknown equations. my first thought was that the numbers would have a distinct product – (n-1)!
if we divide the total product by the (n-1)! product, we would get another equation
c2 = ab
we could then solve the two equations to get them into quadratic formula notation
0 = ax^2 + bx + c
and solve for the two values of x. this answer is correct but factorial grows really fast.

some sort of sum would be better. the sum of the squares from n-1 to 1 would work. that would yield a function of the form
c2 = a^2 + b^2
which could also be solved by using the quadratic equation.

i think its fine to remind someone of the quadratic equation… (maybe only because i myself had to look it up to solve the problem) i mean really though, the last time i used it was probably in 10th grade. as long as they get the idea that given two unknowns and two equations you can solve for the unknowns – thats the point.

quadractic formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a

💡Strategies for Solving This Problem

Two Sum Variant

This is usually "find two numbers that sum to target." It's the most common interview question - I've gotten it or variants at Google, Facebook, and Amazon.

The Core Pattern

For each number, ask: "Have I seen (target - current) before?" If yes, found the pair. If no, remember current number.

This is the hash map pattern - you'll use it constantly.

Approach 1: Hash Map (Standard)

Single pass through array. O(n) time, O(n) space.

This is what 90% of interviewers expect. Know this cold.

Approach 2: Two Pointer (If Sorted)

If array is sorted (or you can sort it): two pointers from start and end. Move left if sum too small, move right if sum too large.

O(n log n) for sorting + O(n) for two pointers = O(n log n) time, O(1) space.

Approach 3: Brute Force (Don't Do This)

Check every pair with nested loops. O(n²) time. Only acceptable if you're completely blanking and need to start somewhere.

Common Follow-ups

  • "Return indices, not values" - slight variation
  • "Find all pairs, not just one" - same approach, don't return early
  • "What if array has duplicates?" - handle carefully
  • "Can you use less space?" - two pointer if sorting is allowed
Scroll to Top