class Solution {
public:
    /**
     * max length of the subarray sum = k
     * @param arr int整型vector the array
     * @param k int整型 target
     * @return int整型
     */
 // https://blog.csdn.net/weixin_43982698/article/details/107135036
    int maxlenEqualK(vector<int>& arr, int k) {
        int len = arr.size();
        unordered_map<int, int>mp;
        mp[0] = -1;
        int sum = 0, res = 0;
        for (int i = 0; i < len; i++) {
            sum += arr[i];
            if (mp.find(sum) == mp.end()) {
                mp[sum] = i;
            }
            if (mp.find(sum - k) != mp.end()) {
                res = max(res, i - mp[sum - k]);
            }
        }
        return res;
    }
};