---
# System prepended metadata

title: 1983. Widest Pair of Indices With Equal Range Sum
tags: [Leetcode, HashMap, Prefix Sum, Medium]

---

# 1983. Widest Pair of Indices With Equal Range Sum
###### tags: `Leetcode` `Medium` `Prefix Sum` `HashMap`
Link: https://leetcode.com/problems/widest-pair-of-indices-with-equal-range-sum/
## 思路
和[0525. Contiguous Array](https://hackmd.io/gvM6GJYHStmB-OH0bKr8sQ)的思路一样 把一个array的1变成-1 然后算两个array一起的prefixSum 如果这个sum之前出现过 说明subarray的和为0 两个subarray里面的1和0个数一样
还有一种方法是直接算```nums1[i]-nums2[i]```(https://leetcode.com/problems/widest-pair-of-indices-with-equal-range-sum/discuss/1429837/Java-Short-Solution-with-Prefix-sum.-O(n)-time-and-memory)
## Code
```java=
class Solution {
    public int widestPairOfIndices(int[] nums1, int[] nums2) {
        for(int i=0; i<nums1.length; i++){
            if(nums1[i]==1) nums1[i]=-1;
        }
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0,-1);
        int len = 0, sum = 0;
        for(int i=0; i<nums1.length; i++){
            sum += nums1[i]+nums2[i];
            if(map.containsKey(sum)){
                len = Math.max(len, i-map.get(sum));
            }
            else{
                map.put(sum, i);
            }
        }
        return len;
    }
}
```