# Subarray sum?

I know this is a easy problem but I am not able to return the vector we need to return the list
Given an unsorted array A of size N that contains only non-negative integers, find a continuous sub-array which adds to a given number S
You don’t need to read input or print anything. The task is to complete the function
subarraySum () which takes arr, N and S as input parameters and returns a list containing the starting and ending positions of the first such occurring subarray from the left where sum equals to S. The two indexes in the list should be according to 1-based indexing. If no such subarray is found, return an array consisting only one element that is -1

class Solution
{
public:
//Function to find a continuous sub-array which adds up to a given number.
vector subarraySum(int arr[], int n, int s)
{
int csum=arr[0];
int st=0;
vectorv;
for(int i=1;i<=n;i++)
{
if(i<n)
csum=csum+arr[i];
while(csum>s&& st<i-1)
{
csum = csum - arr[st];
st++;
}
if(csum==s)
{
v.push_back(st);
v.push_back(i-1);
return v;
}

``````    }

}
``````

};

input- N = 5, S = 12 A[] = {1,2,3,7,5}
Output: 2 4
my output 1 2

Hint:- Use two pointer technique

I think you are confused with the ordering of your conditions which are inside the for loop.
Correct order of conditions may be:
for (int i = 1; i <= n; i++)
{
while (csum > s && st < i - 1)
{
csum = csum - arr[st];
st++;
}
if (csum == s)
{
v.push_back(st);
v.push_back(i - 1);
return v;
}
if (i < n)
csum = csum + arr[i];
}