int sum = 0; int result = 0; Map<Integer, Integer> dict = new HashMap<>(); dict.put(0, -1); for (int i = 0; i < nums.length; i++) { sum += (nums[i] << 1) - 1; if (dict.containsKey(sum)) { result = Math.max(result, i - dict.get(sum)); } else { dict.put(sum, i); } } return result; } }