Skip to content

Commit

Permalink
Create 525. Contiguous array
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored Mar 16, 2024
1 parent 9b6728d commit bdfe652
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 525. Contiguous array
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//525. Contiguous array

class Solution {
public int findMaxLength(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
int sum = 0;
int maxLen = 0;
map.put(0, -1);

for(int i = 0; i < nums.length; i++) {
if(nums[i] == 0) {
sum--;
}

else {
sum += nums[i];
}

if(map.containsKey(sum)) {
maxLen = Math.max(maxLen, i - map.get(sum));
}

else {
map.put(sum, i);
}
}

return maxLen;
}
}

0 comments on commit bdfe652

Please sign in to comment.