#K10211. Maximum Non-Overlapping Subsegments
Maximum Non-Overlapping Subsegments
Maximum Non-Overlapping Subsegments
You are given a sequence of positive integers representing the energy levels of stones and a target energy value \(T\). Your task is to determine the maximum number of non-overlapping contiguous subsegments (subarrays) from the sequence whose sum is exactly \(T\).
Problem Details:
- Each subsegment must consist of consecutive elements.
- Subsegments must not overlap.
- You should maximize the count of such subsegments.
Input Example:
7 8 1 2 3 4 5 3 5
In the above example, the total number of stones is 7, and the target sum \(T = 8\). The output is 1 since only one contiguous subsegment sums exactly to 8.
Note: The input will consist of multiple test cases. The format is described below.
inputFormat
The first line of input contains a single integer \(t\) representing the number of test cases. Each test case consists of two lines:
- The first line contains two integers \(n\) and \(T\), where \(n\) is the number of stones, and \(T\) is the target energy value.
- The second line contains \(n\) positive integers, representing the energy levels.
For example:
3 7 8 1 2 3 4 5 3 5 5 7 1 2 3 4 5 8 3 1 1 1 1 1 1 1 1
outputFormat
For each test case, output a single integer on a new line indicating the maximum number of non-overlapping subsegments whose sum equals \(T\).
Output Example:
1 1 2## sample
3
7 8
1 2 3 4 5 3 5
5 7
1 2 3 4 5
8 3
1 1 1 1 1 1 1 1
1
1
2
</p>