Maximum Population Year Solution
This above problem is taken from
https://leetcode.com/problems/maximum-population-year/
Solution:-
Approach:-
Code:-
public static int methodeName(int[][] arr) { //defined one array years with size 2051 int[] years = new int[2051]; //Now hover over the goven 2d array for (int[] log : arr) { //given array is {1952,1961},{1953,1961 } //so for first iteration log 0 1952 //years all element value is zero. the element at the position years[log[0]] += 1; System.out.println("the element " + years[log[0]]); //1952 assigned to 1. years[log[1]] -= 1; System.out.println("the element is " + years[log[1]]); } int count = 0; int max = Integer.MIN_VALUE; System.out.println("The max is" + max); int result = -1; System.out.println(Arrays.toString(years)); for (int i=1950; i<2051 ; i++) { count += years[i]; //given array is {1952,1961},{1953,1961 } System.out.println("When the year is " + i + " then the value is years is " + years[i]); System.out.println("count is" + count); // System.out.println(Arrays.toString(years)); if (count>max) { max = count; System.out.println("max is " + max); result = i; } } return result; }