Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

Boss fightThe Population Ledger

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Turn ranges into boundary events: The Population Ledger.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Difference-array sweep matches the clues
  2. 2Keep this true after every move: after processing year y, active equals the population alive during y
  3. 3Reach the result within O(n + year range)

Rules and pressure

  • Target cost: O(n + year range)
  • State rule: after processing year y, active equals the population alive during y
Lesson 1 of 3

Live algorithm trace

Difference-array sweep

Complete execution
1 of 7

Record a positive event at each birth and a negative event at each exclusive death.

1events[birth] += 1
2events[death] -= 1
3active = 0
4scan years in order
5active += events[year]
6update answer only when active > best
7return earliest best year
events = 1993:+1, 1999:-1, 2000:+1, 2010:-1
Truth to preserve / Cost target
Truth to preserve

after processing year y, active equals the population alive during y

Cost target

O(n + year range)

A person contributes from birth through the year before death. Add one at birth, subtract one at death, then prefix-sum years to recover population and keep the earliest maximum.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead