c++ You should learn line sweep algorithm
You can do this question in o(n^2) easily but here is a tricky solution which is worth to learn.\n\nstep 1 create an array of 2051 look at constrains to underst
You can do this question in o(n^2) easily but here is a tricky solution which is worth to learn.\n\nstep 1 create an array of 2051 look at constrains to understand why.\n\nstep 2 traverse through the array and mark starting index with +1 and ending index with -1\n\nstep 3 calculate prefix sum of the array\n\nstep 5 then again traverse the array and check the maximum index.\n\nstep 6 return index that contain maximum value.\n\nTime complexity O(N)\n\n```\nclass Solution {\npublic:\n    int maximumPopulation(vector<vector<int>>& logs) {\n        \n        vector<int>v(2051,0);\n        \n        for(auto a:logs){\n            v[a[0]]+=1;\n            v[a[1]]-=1;\n        }\n        for(int i=1;i<2051;i++){\n            v[i]+=v[i-1];\n        }\n        \n        int a=INT_MIN,ans=0;\n        for(int i=0;i<2051;i++){\n            if(a<v[i])a=v[i],ans=i;\n        }\n        return ans;\n    }\n};\n```
[]