category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

525. Contiguous Array, prefix sums, map, medium

Implementation ```cpp int findMaxLength(vector& nums) { int n = nums.size(); map<int, int> mp; int count = 0; int ans = 0; mp[0] = 0; for (int i= 0; i < n; i++) { nums[i] == 0 ? count -- : count ++; (mp.count(count)) ? ans = max(ans, i + 1 - mp[count]): mp[count] = i + 1; } return ans; } ``` </details>
Java Implementation ```cpp class Solution { public int findMaxLength(int[] nums) { Map<Integer, Integer> track = new HashMap<>(); track.put(0, -1); int ans = 0; int count = 0; for (int i = 0; i < nums.length; i++) { count += (nums[i] == 0 ? -1 : 1); if (track.containsKey(count)) { ans = Math.max(i - track.get(count), ans); } else { track.put(count, i); } } return ans; } } ```