#K78272. Find Subarray with Given Sum
Find Subarray with Given Sum
Find Subarray with Given Sum
You are given an array of integers and a target sum \(S\). Your task is to determine whether there exists a contiguous subarray (i.e. a sequence of consecutive elements) whose sum is exactly \(S\). If such a subarray exists, output its starting and ending indices (1-indexed). If there are multiple possible subarrays, output the one that appears first. If no such subarray exists, output -1
.
Note: The array may contain both positive and negative numbers.
Example:
For the array [1, 2, 3, 7, 5] and target sum 12, the answer is "2 4" because \(2+3+7=12\).
inputFormat
The first line of input contains an integer \(T\) denoting the number of test cases.
Each test case consists of two lines:
- The first line contains two integers \(n\) and \(S\) where \(n\) is the size of the array and \(S\) is the target sum.
- The second line contains \(n\) space-separated integers representing the elements of the array.
outputFormat
For each test case, output the starting and ending indices (separated by a space) of the contiguous subarray whose sum equals \(S\). If no such subarray exists, output -1
.
Each result should be printed on a new line.
## sample2
5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10
2 4
1 5
</p>