01
02
03
04
05
06
07
08
09
10
11
A
A2Z Sheet

71. Find Missing Number & The Lost Raffle Ticket

Easy
Step 3: Solve Problems on Arrays [Easy -> Medium -> Hard]›Easy

Find Missing Number & The Lost Raffle Ticket

EasyFunction: findMissingNumber()
ASCI Mission Breakdown • Simple as Hell
"Identify the one number missing from the range [0, n] in the array."
Real-World Metaphor:

Think of a set of pool balls numbered 0 to 9. You count their total weight. You know what the complete set should weigh using Gauss's formula (n * (n + 1) / 2). The weight missing from your scale reveals exactly which ball fell off the table!

Interactive Visual WalkthroughFLOW-DIAGRAM
Step 1 / 3
Expected Sum6
Evaluating

1. Calculate Expected Sum (0 to 3)

Formula: 3 * (3 + 1) / 2 = 6. Expected total of all tickets is 6.

How to Think About This (Mental Model)

  1. Calculate expected sum of all numbers from 0 to n: expectedSum = n * (n + 1) / 2.
  2. Calculate actual sum of numbers in the array.
  3. Missing number is simply: expectedSum - actualSum.

### The Mission Imagine a charity raffle where tickets were printed numbered from `0` up to `n`. Exactly **one ticket** was lost in the wind! You are given an array of the remaining tickets. Because every number from `0` to `n` is distinct, determine which single number ticket is missing.

Examples

Example 1
Input: nums = [1,2,3,4,5]
Output: 15

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Topic Tags:
Solve Problems on Arrays [Easy -> Medium -> Hard]EasyfindMissingNumber
14px
Ln 1:Col 1
8 lines•119 chars
Spaces: 2•UTF-8
JS(Node v20.12)