import java.util.*;
public class Solution {
/**
* max length of the subarray sum = k
* @param arr int整型一维数组 the array
* @param k int整型 target
* @return int整型
*/
public int maxlenEqualK (int[] arr, int k) {
// write code here
//key: 下标从0开始的连续子数组和 value:当前数组和对应的下标从0开始最短数组长度
Map<Integer,Integer> hash = new HashMap<>();
hash.put(0,0);
int max = 0;
int count = 0;
for(int i=0;i<arr.length;i++){
count+=arr[i];
if(hash.containsKey(count-k)){//是否存在下标从0开始的连续子数组和为n-k
max = Math.max(i+1-hash.get(count-k),max);
}
hash.put(count,hash.getOrDefault(count,i+1));
}
return max;
}
}