Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- inline void SubArrSum(int v[], int n, int sum)
- {
- int currsum = v[0], j = 0;
- bool existsum = false;
- for (int i = 1; i <= n; i++)
- {
- while (currsum > sum && j < i)
- {
- currsum -= v[j];
- j++;
- }
- if (currsum == sum)
- {
- existsum = true;
- cout << j << ' ' << i - 1 << endl;
- }
- currsum += v[i];
- }
- if (!existsum)
- cout << "Sum not found";
- }
- int main()
- {
- int v[] = {15, 2, 4, 8, 9, 5, 10, 23}, n = 8, sum = 23;
- SubArrSum(v, n, sum);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement