Problem 317
Subarray with Given Sum
Easy · 10 pts · Array, Sliding Window, Prefix Sum
Problem
The array contains non-negative integers. Find a contiguous subarray whose elements add up to exactly S. If several subarrays qualify, choose the one with the smallest starting position, and among those the one that ends first. Print its starting and ending positions using 1-based indexing, or -1 if no subarray has sum S.
Input
The first line contains N and S. The second line contains N non-negative integers.
Output
Print the 1-based start and end positions separated by a space, or -1.
Constraints
1 <= N <= 100000; 0 <= A[i] <= 1000000000; 1 <= S <= 1000000000
Example input
5 12 1 2 3 7 5
Example output
2 4
Explanation
With S = 12 in 1 2 3 7 5, the subarray 2 3 7 at positions 2 to 4 adds up to 12. No subarray starting at position 1 sums to 12, so the answer is 2 4.