#C7518. Subarrays with Given Sum
Subarrays with Given Sum
Subarrays with Given Sum
Given an array of integers and a target sum \(S\), your task is to find all contiguous subarrays whose elements add up exactly to \(S\). For a subarray \(a_i, a_{i+1}, \ldots, a_j\), the condition is:
\(\sum_{k=i}^{j}a_k = S\)
If one or more subarrays meeting the criteria exist, output each subarray on a separate line with its elements separated by a single space. If no such subarray exists, output -1.
Note: A subarray is defined as a contiguous segment of the array. The array might contain duplicate elements, and overlapping subarrays are allowed.
inputFormat
The input is read from stdin and has the following format:
- The first line contains an integer \(N\), the number of elements in the array.
- The second line contains \(N\) space-separated integers representing the array elements. If \(N = 0\), this line may be empty.
- The third line contains an integer \(S\), the target sum.
outputFormat
The output is written to stdout. For each contiguous subarray that sums up to \(S\), print the subarray on a new line with its elements separated by a single space. If no such subarray exists, print -1.
## sample5
1 2 3 4 5
5
2 3
5
</p>