Knowledge Guide
HomeDSAAdvanced Patterns

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:

Example 2:

Example 3:

Constraints:

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, 3 people 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

  1. Initialize the Population Array:

    • Create an array population of size 101 to store population changes for each year from 1950 to 2050.
  2. Record Births and Deaths:

    • For each log in logs:
      • Increment the population at the birth year index: population[log[0] - 1950]++.
      • Decrement the population at the death year index: population[log[1] - 1950]--.
  3. Initialize Variables for Tracking Maximum Population:

    • maxPop to 0: This will keep track of the maximum population encountered.
    • maxYear to 1950: This will store the year with the maximum population.
    • currentPop to 0: This will track the current population while iterating through the years.
  4. Calculate Maximum Population Year:

    • For each year from 0 to 100:
      • Add the population change for the current year to currentPop: currentPop += population[year]. It adds positive or negative value to the currentPop based on the number of births or deaths in the year.
      • If currentPop is greater than maxPop:
        • Update maxPop to currentPop.
        • Update maxYear to the current year: maxYear = 1950 + year.
  5. Return the Result:

    • Return maxYear, which is the year with the maximum population.

Algorithm Walkthrough

Using the input: [[1980, 1990], [1975, 1985], [1985, 1995], [1990, 2000], [1999, 2020], [1994, 2032]]

Image
Image
  1. Initialization:

    • population array of size 101 is initialized to all zeros.
  2. Processing logs:

    • For [1980, 1990]:
      • Increment population[30] by 1. (population[1980 - 1950])
      • Decrement population[40] by 1. (population[1990 - 1950])
    • For [1975, 1985]:
      • Increment population[25] by 1. (population[1975 - 1950])
      • Decrement population[35] by 1. (population[1985 - 1950])
    • For [1985, 1995]:
      • Increment population[35] by 1. (population[1985 - 1950])
      • Decrement population[45] by 1. (population[1995 - 1950])
    • For [1990, 2000]:
      • Increment population[40] by 1. (population[1990 - 1950])
      • Decrement population[50] by 1. (population[2000 - 1950])
    • For [1999, 2020]:
      • Increment population[49] by 1. (population[1999 - 1950])
      • Decrement population[70] by 1. (population[2020 - 1950])
    • For [1994, 2032]:
      • Increment population[44] by 1. (population[1994 - 1950])
      • Decrement population[82] by 1. (population[2032 - 1950])

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]
  1. Finding the maximum population year:

    • Year 1950:
      • currentPop = 0
    • Year 1975:
      • currentPop += population[25] (1) -> currentPop = 1
      • maxPop = 1, maxYear = 1975
    • Year 1980:
      • currentPop += population[30] (1) -> currentPop = 2
      • maxPop = 2, maxYear = 1980
    • Year 1985:
      • currentPop += population[35] (0) -> currentPop = 2
      • maxPop = 2, maxYear = 1980
    • Year 1990:
      • currentPop += population[40] (0) -> currentPop = 2
    • Year 1994:
      • currentPop += population[44] (1) -> currentPop = 3
      • maxPop = 3, maxYear = 1994
    • Year 1995:
      • currentPop += population[45] (-1) -> currentPop = 2
      • maxPop = 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
  2. Final result:

    • The earliest year with the maximum population is 1994.

Code

java
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 , where (N) is the number of entries in the logs. This is because we iterate through the logs to populate the population changes and then iterate through the years (which is a constant number of iterations, to find the maximum population.

Space Complexity

The space complexity is (constant space) because we use a fixed-size array of 101 elements to store the population changes for each year. The size of this array does not change with the input size.

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

🪜 Hint ladder (no spoilers)

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.
🎨 Explain the approach visually

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.
🔍 Review my solution

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.
🔁 Drill the pattern

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.

📝 My notes