#K74162. Find the Contiguous Subarray with a Given Sum

    ID: 34137 Type: Default 1000ms 256MiB

Find the Contiguous Subarray with a Given Sum

Find the Contiguous Subarray with a Given Sum

You are given an array of positive integers and two integers N and S. Your task is to find a contiguous subarray of the given array whose sum is exactly equal to S. If such a subarray is found, output the starting and ending indices (0-indexed). If there are multiple possible answers, output the one with the smallest starting index. If no such subarray exists, output -1.

The key idea to solve this problem is to use the sliding window technique. As the array contains only positive integers, you can maintain a current sum and adjust the window boundaries dynamically.

The mathematical formulation of the problem can be summarized as follows:

Given an array \(A = [a_0, a_1, \dots, a_{N-1}]\) and a target sum \(S\), find indices \(i\) and \(j\) such that \[ a_i + a_{i+1} + \dots + a_j = S \] . If such indices do not exist, return -1.

inputFormat

The first line contains two integers N and S, where N is the number of elements in the array and S is the target sum. The second line contains N space-separated positive integers representing the elements of the array.

outputFormat

If a contiguous subarray exists whose sum is equal to S, output two space-separated integers representing the starting and ending indices (0-indexed) of that subarray. If no such subarray exists, output -1.

## sample
8 15
1 2 3 7 5 3 2 1
2 4

</p>