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:
✅ 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
🤖 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.