GeeksforGeeks Medium

Longest Subarray with Sum K

Given an array (may contain negatives) and a target k, return the length of the longest subarray whose elements sum to exactly k.

arrayprefix-sumhash-table
Open on GeeksforGeeks ↗
02

Intuition

💡

Every subarray sum is a difference of two prefix sums: sum(i..j) = pre[j] − pre[i−1]. So a subarray sums to k exactly when the current prefix minus some earlier prefix equals k. Store the first index where each prefix value occurred, and each new prefix asks: have I seen prefix − k before?

03

Approach

1

Why sliding window fails here

With negatives, growing the window can shrink the sum and shrinking can grow it — the window has no monotone signal to steer by. We need something that works for arbitrary signs: prefix sums.

2

Prefix sums turn subarrays into pairs

Walk once keeping the running sum pre. If some earlier prefix equaled pre − k, the elements after that point sum to exactly k. A hash map from prefix value → earliest index answers that in O(1).

3

Keep only the first occurrence

For the longest subarray we want the earliest index where each prefix value appeared, so never overwrite an existing entry. Seed the map with {0: -1} so subarrays starting at index 0 are counted.

04

Solution & live demo

python
1def longest_subarray_sum_k(nums, k):
2 first = {0: -1} # prefix value -> earliest index
3 pre = 0
4 best = 0
5 for i, n in enumerate(nums):
6 pre += n
7 if pre - k in first:
8 best = max(best, i - first[pre - k])
9 if pre not in first:
10 first[pre] = i
11 return best
05

Edge cases

Subarray starts at index 0

The seed {0: -1} makes pre == k yield length i − (−1) = i + 1.

Prefix value repeats

Keep the first index only — a later duplicate would only shorten the candidate subarray.

06

Complexity

Time
O(n)
Space
O(n)
One pass; the map holds at most n+1 prefix values.