easy Maximum Population Year
Problem Statement
You are given a 2D integer array logs containing the birth and death years of n people. In logs array, logs[i] = [birthi, deathi] indicates the birth and death years of the ith person.
The population of a year is the number of people alive during that year. The ith person is counted in year x's population if x is in the inclusive range [birthi, deathi - 1].
Return the earliest year with the highest population.
Examples
Example 1:
- Input: logs = [[1980, 1990], [1975, 1985], [1985, 1995], [1990, 2000], [1999, 2020], [1994, 2032]]
- Expected Output:
1994 - Explanation: The population in the year 1994 was the highest. In that year,
3people were alive (born in 1985, 1990, and 1994).
Example 2:
- Input: logs = [[1970, 1990], [1980, 2009], [1960, 1970], [1959, 1982]]
- Expected Output:
1980 - Explanation: The population in the year 1980 was the highest. In that year, three people were alive (born in 1970, 1980, and 1959).
Example 3:
- Input: logs = [[2000, 2010], [2005, 2015], [2010, 2020], [2015, 2025]]
- Expected Output:
2005 - Explanation: The population in the year 2005 was the highest. In that year, two people were alive (born in 2000, and 2005).
Constraints:
- 1 <= logs.length <= 100
- 1950 <= birthi < deathi <= 2050
Try it yourself
Try solving this question here:
🎯 STRICT STANDOUT — Maximum Population Year — problem
1. Why / judgment
Alive in year x iff birth ≤ x ≤ death−1. Naïve per-year loop over people is O(n·Y). Difference array: +1 at birth, −1 at death, then prefix scan — classic sweep on a fixed calendar. Judgment: when events are on a small dense timeline, counting/sweep beats sorting events for constant factors and clarity.
2. Big-O derivation (K11)
Years 1950..2050 → range R=101 fixed.
Build: O(n) updates; scan: O(R) prefix.
Time O(n+R)=O(n); space O(R)=O(1) for fixed range.
If years were unbounded huge → sort events O(n log n) sweep instead.
3. Pattern + when-NOT (K12)
Name: DIFFERENCE ARRAY / LINE SWEEP ON BOUNDED TIMELINE
Recognition: interval birth/death; ask max concurrent + earliest year.
When-NOT: Need arbitrary huge years → event list + sort. Need max concurrent with offline queries many ranges → segment tree. Inclusive death (if problem counted death year) → off-by-one: decrement at death+1.
4. Edge hand-run (K13)
[[2000,2010]] only → max year 2000 pop 1.
[[1950,1951],[1950,1952]] → 1950 has 2.
Empty not allowed by constraints (n≥1).
5. Interviewer follow-ups (model answers)
Q1. Why decrement at death not death−1?
A: Person is NOT alive in death year; so population drops at the start of death year = index death.
Q2. Why earliest year on ties?
A: Scan left→right; update max only on strict greater population.
Q3. HashMap of years instead of array?
A: Works but denser array is cleaner for [1950,2050]; map needs sorting keys for scan.
6. Short drills
Drill: logs=[[1970,1990],[1980,2009],[1960,1970],[1959,1982]] → 1980.
Drill: state off-by-one if death counted inclusive.
✅ Solution Maximum Population Year
Problem Statement
You are given a 2D integer array logs containing the birth and death years of n people. In logs array, logs[i] = [birthi, deathi] indicates the birth and death years of the ith person.
The population of a year is the number of people alive during that year. The ith person is counted in year x's population if x is in the inclusive range [birthi, deathi - 1].
Return the earliest year with the highest population.
Examples
Example 1:
- Input: logs = [[1980, 1990], [1975, 1985], [1985, 1995], [1990, 2000], [1999, 2020], [1994, 2032]]
- Expected Output:
1994 - Explanation: The population in the year 1994 was the highest. In that year,
3people were alive (born in 1985, 1990, and 1994).
Example 2:
- Input: logs = [[1970, 1990], [1980, 2009], [1960, 1970], [1959, 1982]]
- Expected Output:
1980 - Explanation: The population in the year 1980 was the highest. In that year, three people were alive (born in 1970, 1980, and 1959).
Example 3:
- Input: logs = [[2000, 2010], [2005, 2015], [2010, 2020], [2015, 2025]]
- Expected Output:
2005 - Explanation: The population in the year 2005 was the highest. In that year, two people were alive (born in 2000, and 2005).
Constraints:
- 1 <= logs.length <= 100
- 1950 <= birthi < deathi <= 2050
Solution
To solve this problem, we can use a counting approach to keep track of population changes over the years. This approach involves using a single array to count the number of people born and another to count the number of people who have died. By iterating through these arrays, we can compute the population for each year and determine the year with the highest population.
This approach is efficient because it avoids the need to sort the years, making it faster. Instead, we directly compute the population changes and keep track of the maximum population year. This counting method ensures that we process each birth and death year once, leading to an optimal solution.
Step-by-Step Algorithm
-
Initialize the Population Array:
- Create an array
populationof size 101 to store population changes for each year from 1950 to 2050.
- Create an array
-
Record Births and Deaths:
- For each
loginlogs:- Increment the population at the birth year index:
population[log[0] - 1950]++. - Decrement the population at the death year index:
population[log[1] - 1950]--.
- Increment the population at the birth year index:
- For each
-
Initialize Variables for Tracking Maximum Population:
maxPopto 0: This will keep track of the maximum population encountered.maxYearto 1950: This will store the year with the maximum population.currentPopto 0: This will track the current population while iterating through the years.
-
Calculate Maximum Population Year:
- For each
yearfrom 0 to 100:- Add the population change for the current year to
currentPop:currentPop += population[year]. It adds positive or negative value to thecurrentPopbased on the number of births or deaths in theyear. - If
currentPopis greater thanmaxPop:- Update
maxPoptocurrentPop. - Update
maxYearto the current year:maxYear = 1950 + year.
- Update
- Add the population change for the current year to
- For each
-
Return the Result:
- Return
maxYear, which is the year with the maximum population.
- Return
Algorithm Walkthrough
Using the input: [[1980, 1990], [1975, 1985], [1985, 1995], [1990, 2000], [1999, 2020], [1994, 2032]]
-
Initialization:
populationarray of size 101 is initialized to all zeros.
-
Processing logs:
- For
[1980, 1990]:- Increment
population[30]by 1. (population[1980 - 1950]) - Decrement
population[40]by 1. (population[1990 - 1950])
- Increment
- For
[1975, 1985]:- Increment
population[25]by 1. (population[1975 - 1950]) - Decrement
population[35]by 1. (population[1985 - 1950])
- Increment
- For
[1985, 1995]:- Increment
population[35]by 1. (population[1985 - 1950]) - Decrement
population[45]by 1. (population[1995 - 1950])
- Increment
- For
[1990, 2000]:- Increment
population[40]by 1. (population[1990 - 1950]) - Decrement
population[50]by 1. (population[2000 - 1950])
- Increment
- For
[1999, 2020]:- Increment
population[49]by 1. (population[1999 - 1950]) - Decrement
population[70]by 1. (population[2020 - 1950])
- Increment
- For
[1994, 2032]:- Increment
population[44]by 1. (population[1994 - 1950]) - Decrement
population[82]by 1. (population[2032 - 1950])
- Increment
- For
Final population array values at key years:
[0: 0, 25: 1, 30: 1, 35: 0, 40: 0, 44: 1, 45: -1, 49: 1, 50: -1, 70: -1, 82: -1]
-
Finding the maximum population year:
- Year 1950:
currentPop= 0
- Year 1975:
currentPop+=population[25](1) ->currentPop= 1maxPop= 1,maxYear= 1975
- Year 1980:
currentPop+=population[30](1) ->currentPop= 2maxPop= 2,maxYear= 1980
- Year 1985:
currentPop+=population[35](0) ->currentPop= 2maxPop= 2,maxYear= 1980
- Year 1990:
currentPop+=population[40](0) ->currentPop= 2
- Year 1994:
currentPop+=population[44](1) ->currentPop= 3maxPop= 3,maxYear= 1994
- Year 1995:
currentPop+=population[45](-1) ->currentPop= 2maxPop= 3,maxYear= 1994
- Year 1999:
currentPop+=population[49](1) ->currentPop= 3
- Year 2000:
currentPop+=population[50](-1) ->currentPop= 2
- Year 2020:
currentPop+=population[70](-1) ->currentPop= 1
- Year 2032:
currentPop+=population[82](-1) ->currentPop= 0
- Year 1950:
-
Final result:
- The earliest year with the maximum population is 1994.
Code
import java.util.Arrays;
class Solution {
public int maximumPopulation(int[][] logs) {
int[] population = new int[101]; // Array to store population changes
for (int[] log : logs) {
population[log[0] - 1950]++; // Increment population at birth year
population[log[1] - 1950]--; // Decrement population at death year
}
int maxPop = 0;
int maxYear = 1950;
int currentPop = 0;
// Calculate maximum population year
for (int year = 0; year < 101; year++) {
currentPop += population[year];
if (currentPop > maxPop) {
maxPop = currentPop;
maxYear = 1950 + year;
}
}
return maxYear;
}
public static void main(String[] args) {
Solution sol = new Solution();
int[][] logs1 = {
{ 1980, 1990 },
{ 1975, 1985 },
{ 1985, 1995 },
{ 1990, 2000 },
{ 1999, 2020 },
{ 1994, 2032 },
};
int[][] logs2 = {
{ 1970, 1990 },
{ 1980, 2009 },
{ 1960, 1970 },
{ 1959, 1982 },
};
int[][] logs3 = {
{ 2000, 2010 },
{ 2005, 2015 },
{ 2010, 2020 },
{ 2015, 2025 },
};
System.out.println(sol.maximumPopulation(logs1)); // Output: 1994
System.out.println(sol.maximumPopulation(logs2)); // Output: 1980
System.out.println(sol.maximumPopulation(logs3)); // Output: 2005
}
}
Complexity Analysis
Time Complexity
The time complexity of the algorithm is
Space Complexity
The space complexity is
🎯 STRICT STANDOUT — Solution Maximum Population Year
1. Why / judgment
Offset years by 1950 into a length-101 delta array. Prefix sum is live population. Strictly greater updates both maxPop and maxYear — ties keep the earlier year automatically.
2. Big-O derivation (K11)
Build O(n): each log two ±1.
Prefix O(101): cur += delta[i]; if cur>max → record year.
Hand-run:
logs=[[1980,1990],[1975,1985],[1985,1995],[1990,2000],[1999,2020],[1994,2032]]
After deltas + prefix, peak cur=3 at year 1994 ✓
Second sample peaks at 1980 with 3 ✓
3. Pattern + when-NOT (K12)
Name: BOUNDED DIFFERENCE ARRAY
Recognition: fixed year window + concurrency max.
When-NOT: Variable huge span → do not allocate maxYear−minYear if that is 1e9; sort events.
4. Edge hand-run (K13)
Single person [1999,2000] → only 1999 has 1.
Death year same as next birth: person A dies 1985, B born 1985 → A not counted in 1985.
All disjoint intervals → maxPop=1, earliest birth among them.
5. Interviewer follow-ups (model answers)
Q1. Prove O(1) space
A: Array length fixed 101 independent of n (n≤100 anyway).
Q2. Sort+scan alternative complexity?
A: O(n log n) to sort 2n events; same answer, worse for small fixed range.
Q3. What if death inclusive?
A: Then decrement at death+1 — wrong for this statement; unit-test [1950,1951]→only 1950.
6. Short drills
Drill: recompute example 3 → 2005 with pop 2.
Drill: implement with events list and verify same year.
Recognize it: Membership / frequency / “have I seen this?” in O(1) → a hash map or set.
▶ Visualize this problem (step it, predict each fork)
🤖 Don't fully get this? Learn it with Claude
Stuck on Maximum Population Year? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Maximum Population Year** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Maximum Population Year** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Maximum Population Year**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Maximum Population Year**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.