#C5757. Consecutive Bag Sum
Consecutive Bag Sum
Consecutive Bag Sum
You are given an array of n positive integers where each integer represents the amount of money in a bag. Your task is to find a consecutive subarray (i.e. a contiguous segment of the array) whose sum is exactly equal to a given target value k.
If there are multiple subarrays fulfilling this requirement, choose the one with the smallest starting index. In case no such subarray exists, output -1
.
Note: The array is 1-indexed, meaning that the first element of the array is indexed as 1.
The input starts with an integer t that denotes the number of test cases. For each test case, you will be provided with the size of the array (n), the target sum (k), followed by n space-separated integers representing the money in each bag.
inputFormat
The first line contains a single integer t denoting the number of test cases.
For each test case, the first two integers are n and k, representing the number of bags and the target sum respectively, followed by n space-separated positive integers which represent the amounts in the bags.
outputFormat
For each test case, output a single line containing two space-separated integers: the starting and ending indices (1-indexed) of the subarray whose sum equals k. If no such subarray exists, output -1
.
3
5 12 1 2 3 7 5
5 15 1 2 3 4 5
10 55 1 2 3 4 5 6 7 8 9 10
2 4
1 5
1 10
</p>