Skip to the code
+25 XP

#48 Longest Streak Hitting a Target

medium

A fitness app stores daily calorie balance changes in nums, which can be negative. The coach wants the longest stretch of back-to-back days whose changes add up to exactly k.

Write longestTargetStreak(nums, k) that returns the length of that stretch, or 0 if no stretch adds up to k.

Example 1

Input: nums = [2,-1,3,-2,4,1], k = 4
Output: 4

Days 1 to 4, [-1, 3, -2, 4], add up to 4. No longer stretch does.

Example 2

Input: nums = [1,2,3], k = 7
Output: 0

No stretch adds up to 7.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • -10^9 <= k <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).