Longest Subarray with Sum K

medium

You are given an integer array arr — it may contain positives, negatives, and zeros — and an integer k.

Find the length of the longest subarray whose elements sum to exactly k. A subarray is a contiguous block of the array.

If no subarray sums to k, return 0.

Hints

A subarray is defined by where it starts and where it ends. What single quantity, computed once left to right, gives you any subarray's sum in O(1)?
If the running total up to index j is S, then a subarray ending at j sums to k exactly when some earlier running total equals S - k.
Store each prefix sum's earliest index in a hash map, seeded with 0 -> -1. At each index look up sum - k; the index gap is a candidate length.

Common doubts

Sliding windows need the invariant grow → sum rises, shrink → sum falls, which only holds when all elements are non-negative. With negatives, growing the window can make the sum drop, so there is no correct rule for when to expand or shrink — the window silently skips valid answers.
We want the longest subarray. For a fixed prefix sum value, the earliest index maximizes the gap i - first[s - k]. Overwriting with a later index can only shorten future answers.
It represents the empty prefix before the array starts. Without it, any subarray beginning at index 0 (where prefix[j] itself equals k) is never detected — arr = [15], k = 15 would wrongly return 0.
No — the same logic works. A zero-sum subarray appears exactly when a prefix sum value repeats, and the earliest-occurrence map captures that automatically.

Interview follow-ups

Same prefix-sum idea, but the map stores how many times each prefix sum has occurred instead of its earliest index. At each step add count[s - k] to the answer, then increment count[s] — still one pass, O(n).
Yes — with non-negative elements the running sum is monotonic, so a two-pointer sliding window works in O(n) time and O(1) space: extend the right end, shrink from the left while the sum exceeds k.
Replace every 0 with -1; now you need the longest subarray with sum 0 — this exact algorithm with k = 0.

Fun facts

  • The prefix-sum + hash-map trick collapses a question about pairs of positions (n²/2 candidates) into n single-value lookups — a dimensionality drop that powers a whole family of subarray problems.
  • One skeleton, many disguises: set k = 0 and you detect zero-sum stretches; map 0 → -1 in a binary array and you solve the longest balanced run of 0s and 1s; swap earliest-index for a counter and you count subarrays instead of measuring them.

Asked at

AmazonGoogleMicrosoftAdobe
Frequently Sometimes Occasionally
Example 1
Input: arr = [10, 5, 2, 7, 1, -10], k = 15
Output: 6
Three subarrays sum to 15: [10, 5], [5, 2, 7, 1], and the whole array [10, 5, 2, 7, 1, -10]. The longest has length 6.
Example 2
Input: arr = [-5, 8, -14, 2, 4, 12], k = -5
Output: 5
[-5] and [-5, 8, -14, 2, 4] both sum to -5. The longest has length 5.
Example 3
Input: arr = [10, -10, 20, 30], k = 5
Output: 0
No subarray sums to 5, so the answer is 0.
Constraints

- 1 <= arr.size <= 10^5 - -10^4 <= arr[i] <= 10^4 - -10^9 <= k <= 10^9

Solve this problem →