Find the Missing Number in a Range

Medium ⏱ 12 min 59% acceptance ★★★★★ 4.8
Write find_missing(numbers, n) where numbers contains n - 1 distinct integers from 1 to n inclusive (exactly one is missing). Return the missing integer using the sum-formula technique rather than searching.

Examples

Example 1
Input
numbers = [1, 2, 4, 5], n = 5
Output
3
Explanation

The full 1..5 sum is 15; numbers sums to 12; 15 - 12 = 3.

Constraints

  • len(numbers) == n - 1
  • 1 <= n <= 10^6

Topics

ListsMath

Companies

MicrosoftAdobe

Hints

Hint 1

The sum of 1..n is n*(n+1)//2.

Hint 2

Subtract the actual sum of numbers from that expected total.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.