function maxSubArrayLenEqualsToK(arr, k) {
let current = 0;
let len = 0;
let hmap = new Map();
const size = arr.length;
for (let i = 0; i < size; i++) {
current += arr[i];
if (current == k) {
len = i + 1;
}
else if (hmap.has(current - k)) {
len = Math.max(len, i - hmap.get(current - k));
}
if (!hmap.has(current)) {
hmap.set(current, i);
}
}
return len;
}
const arr = [1, -1, 5, -2, -3, 2, 3, 3];
let K = 3;
// 1, -1, 5, -2 = 3 (4)
// 5, -2 = 3 (2)
// -2, -3, 2, 3, 3 = 3 (5)
console.log(maxSubArrayLenEqualsToK(arr, K));
/*
run:
5
*/