Submissions : 20704 Accuracy :


Subarray with given sum

Given an unsorted array of non-negative integers, find a continuous sub-array which adds to a given number.


The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line of each test case is N and S, where N is the size of array and S is the sum. The second line of each test case contains N space separated integers denoting the array elements.


Corresponding to each test case, in a new line, print the starting and ending positions of first such occuring subarray if sum equals to subarray, else print -1.

Note: Position of 1st element of the array should be considered as 1.

Expected Time Complexity: O(n)

1 ≤ T ≤ 100
1 ≤ N ≤ 100
1 ≤ an array element ≤ 200


5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10

2 4
1 5

Explanation :
In first test case, sum of elements from 2nd position to 4th position is 12
In second test case, sum of elements from 1st position to 5th position is 15

**For More Examples Use Expected Output**

Set Default Code

It is recommended to 'Test' your code before clicking 'Submit' button!


Need help with your code? Please use, generate link and share the link here.