KatInterview

Peak Subarray Sum in a Run-Length-Encoded Sequence

Arrays & HashingAuto-graded · 20 test cases
HardAuto-gradedArrays & Hashingsliding-windowprefix-sumsrun-length-encoding

Last updated

Tests whether a candidate can find the maximum sum of a fixed-length window over a run-length-encoded array too large to expand in memory.

Problem Statement

A very long sequence of integers is stored in compressed form as a list of runs. Each run is a pair [value, length], meaning the value value repeats consecutively for length positions. For instance, [[4, 2], [7, 3]] encodes the sequence [4, 4, 7, 7, 7].

The total number of elements can reach 10^15, so the full sequence must never be materialized. Given the compressed representation and a window size k, return the largest sum achievable by any contiguous block of exactly k elements in the underlying sequence.

How the window sum changes

Think about sliding the window one step to the right. The new sum equals the old sum plus the element entering on the right minus the element leaving on the left. That difference stays constant for as long as neither edge of the window crosses a run boundary. Between two consecutive boundary events the window sum is a linear function of the start position, so its maximum on that stretch must occur at one of the two endpoints.

This means you only need to evaluate the window sum at a small set of candidate start positions:

  • Position 0 and position N − k (the first and last valid starts).
  • Every position where a run boundary aligns with the left edge of the window.
  • Every position b − k where a run boundary b aligns with the right edge of the window.

All positions outside [0, N − k] are ignored. The total number of candidates is at most 2M + 4, where M is the number of runs.

Evaluating the sum at a candidate

Precompute two parallel arrays:

  • starts[i] — the index of the first element in run i.
  • prefix_at_start[i] — the total sum of all elements before run i.

Given a position x, binary-search for the run that contains it, then compute:

P(x) = prefix_at_start[i] + value_i * (x - starts[i])

The window sum starting at position s is then P(s + k) − P(s).

Constraints

  • Total sequence length N satisfies 1 ≤ N ≤ 10^15.
  • Number of runs M satisfies 1 ≤ M ≤ 10^5.
  • Each run length is a positive integer.
  • 1 ≤ k ≤ N.
  • Run values may be negative, zero, or positive.
  • Do not materialize the full sequence; solutions that allocate O(N) memory will exceed limits.

Examples

Example 1

Input: max_window_sum([[1, 2], [3, 4], [2, 1]], 3)

Output: 9

Example 2

Input: max_window_sum([[10, 100], [20, 100]], 150)

Output: 2500

Hints

Hint 1

The full sequence is too large to expand. Instead, think about how the window sum changes as you slide it one step at a time — when does that per-step change actually change?

Hint 2

The per-step change is constant between run boundaries at both the left and right edges of the window. That makes the sum a piecewise-linear function of the start position, and a linear function achieves its max at an endpoint — so you only need to check boundary-aligned positions.

Hint 3

Build a prefix-sum function using binary search on precomputed run-start offsets. Then collect all candidate start positions (run boundaries and those shifted left by k), filter to [0, N−k], and evaluate the window sum at each one.