#C7993. Maximum Non-overlapping Subarrays with Sum K
Maximum Non-overlapping Subarrays with Sum K
Maximum Non-overlapping Subarrays with Sum K
You are given an array of integers and an integer k. Your task is to determine the maximum number of non-overlapping subarrays such that the sum of each subarray is exactly equal to k. A subarray is a contiguous part of the array.
Note: Once a valid subarray is found, you cannot use any of its elements in another subarray. Use an efficient strategy to keep track of the cumulative sum and reset when a valid subarray is encountered.
The mathematical interpretation is as follows. Let the array be nums and the cumulative sum up to index i be denoted by S_i. A subarray from index j+1 to i sums to k if and only if:
[ S_i - S_j = k ]
Your goal is to find the maximum count of such subarrays that do not overlap.
inputFormat
The input is read from standard input (stdin). The first line contains two integers n and k where n is the number of elements in the array and k is the target sum. The second line contains n space-separated integers representing the array elements.
outputFormat
Output a single integer to standard output (stdout) which represents the maximum number of non-overlapping subarrays whose sum equals k.## sample
5 3
1 1 1 1 1
1