Java || Beginner friendly (with explanation) || Beats 100%
Approach\n Describe your approach to solving the problem. \n1. We want to analyze a range of years from 1950 to 2050, and we\'ll use an array of size 101 to sto
# Approach\n<!-- Describe your approach to solving the problem. -->\n1. We want to analyze a range of years from 1950 to 2050, and we\'ll use an array of size 101 to store our data.\n\n2. To work with the array more conveniently, we will shift the input years to the range of 0 to 100. For example, for the year 1950, we will store it at index 0, for 1951 at index 1, and so on.\n\n3. For every log entry [birth, death], increment the value in array at index \'birth\' by 1 and decrement the value at index \'death\' by 1.\n\n4. While doing the prefix sum, we will simultaneously find the year with the maximum number of people alive in the range.\n\n5. Finally, the result will be the year with the maximum number of people alive, but we need to convert it back to the original year by adding 1950 to the index.\n\n\n# Code\n```\nclass Solution {\n    public int maximumPopulation(int[][] logs) {\n        int[] arr = new int[101];\n        for(int[] log : logs) {\n            int birth = log[0];\n            int death = log[1];\n            arr[birth-1950]++;\n            arr[death-1950]--;\n        }\n\n        int max = arr[0];\n        int year = 1950;\n        for(int i = 1 ; i < 101; i++) {\n            arr[i] += arr[i-1];\n            if(arr[i] > max) {\n                max = arr[i];\n                year = 1950 + i;\n            }\n        }\n        return year;\n    }\n}\n```\n![WhatsApp Image 2023-07-31 at 11.33.06 PM.jpeg](https://assets.leetcode.com/users/images/7288dd9b-bf65-4e4b-8a0c-15a50e28ec5c_1690826615.9786594.jpeg)\n\n
['Java']