finding a subsequence of given size whose sum is equal to given sum
Answers
Answered by
0
what is the given sum and the given size man!!!!!
Answered by
0
bool subArraySum(int arr[], int n, int sum)
{
int curr_sum = 0, start = 0, i;
bool found = false;
for (i = 0; i <= n; i++)
{
while (curr_sum > sum && start < i)
{
curr_sum = curr_sum - arr[start];
start++;
}
if (curr_sum == sum)
{
cout<<""Sum found in b/w indices: ""<<start<<"" & ""<<(i-1)<<""\n"";
curr_sum -= arr[start];
start++;
found = true;
}
// Add this element to curr_sum
if (i < n) {
curr_sum = curr_sum + arr[i];
}
}
return found;
}
Similar questions