You are viewing a single comment's thread. Return to all comments →
prefixSum + hashMap
function kSub(k: number, nums: number[]): number { const map = new Map<number, number>(); const n = nums.length; const prefix = new Array(n + 1).fill(0); map.set(0, 1); let count = 0; for(let i = 1; i <= n; i++) { prefix[i] = (prefix[i - 1] + nums[i - 1]) % k; count += map.get(prefix[i]) ?? 0; map.set(prefix[i], (map.get(prefix[i]) ?? 0) + 1); } return count; }
Seems like cookies are disabled on this browser, please enable them to open this website
K-Subarrays
You are viewing a single comment's thread. Return to all comments →
prefixSum + hashMap