class Solution {
public:
/**
* max length of the subarray sum = k
* @param arr int整型vector the array
* @param k int整型 target
* @return int整型
*/
int maxlenEqualK(vector<int>& arr, int k) {
// write code here
int sum=0;
map<int,int>m;
m[0] = -1;
int n = arr.size();
int maxLen=INT_MIN;
for(int i=0;i<n;++i) {
sum += arr[i];
if (!m.count(sum)) {
//第一次出现的位置
m[sum] = i;
// maxLen = max(maxLen, i-m[sum-k]);
}
//0-j = sum-k ,那么 k = j+1 ~i
if (m.count(sum-k) && i-m[sum-k] > maxLen)
//
maxLen = i-m[sum-k];
}
return maxLen;
}
};