Maximum Number of Non-Overlapping Subarrays With Sum Equals Target

Given an array nums and an integer target, return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.

Example 1
Inputnums = [1,1,1,1,1], target = 2
Output2
There are 2 non-overlapping subarrays with sum equal to target 2.
Example 2
Inputnums = [-1,3,5,1,4,2,-9], target = 6
Output2
There are 3 subarrays with sum equal to 6, but only the first 2, [5,1] and [4,2], are non-overlapping.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 0 <= target <= 10^6

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate