Subarray with given sum in c++
/*
Geeks For Geeks
link problem: https://practice.geeksforgeeks.org/problems/subarray-with-given-sum-1587115621/1/?page=1&difficulty[]=0&status[]=unsolved&sortBy=submissions#
*/
class Solution
{
public:
//Function to find a continuous sub-array which adds up to a given number.
vector<int> subarraySum(int arr[], int n, long long s)
{
// Your code here
vector<int> ans;
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = 0;
for (int j = i; j < n; j++)
{
sum += arr[j];
if (sum == s)
{
ans.push_back(i + 1);
ans.push_back(j + 1);
return ans;
}
else if (sum > s)
{
break;
}
else
{
continue;
}
}
}
ans.push_back(-1);
return ans;
}
};