Ctrl K

Longest Run with a Given Sum

Medium
You are given an integer array nums (values may be negative) and a number target. A run is a non-empty stretch of consecutive elements of the array. Return the length of the longest run whose elements sum to target, or 0 if there is no such run.
Example 1
nums=[2, -1, 3, 1, -2], target=2 → 3

The runs [2], [-1, 3] and [3, 1, -2] all sum to 2. The longest is [3, 1, -2], of length 3.

Example 2
nums=[4, 4], target=3 → 0

No run sums to 3, so the answer is 0.

Constraints
  • 1 ≤ nums.length ≤ 2·10⁵
  • -10⁴ ≤ nums[i] ≤ 10⁴
  • -10⁹ ≤ target ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Stretch Divisible by k MediumAs Many Heads as Tails MediumStretches With an Exact Total MediumWindows With an Exact Alarm Count MediumWindows With a Divisible Sum Medium
loading Python…
Input
nums=[2, -1, 3, 1, -2], target=2
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 117 tests on the server, hidden ones included.