Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool isSubSum (int arr[], int n, int sum) {
- int curr_sum = arr[0], s = 0;
- for (int e = 1; e <= n; e++) {
- while (curr_sum > sum && s < e-1) {
- curr_sum -= arr[s];
- s++;
- }
- if (curr_sum == sum) {
- return true;
- }
- if (e < n) {
- curr_sum = arr[e];
- }
- }
- return (curr_sum == sum ? true : false);
- }
- int main() {
- int arr[]{7,8,2,4,5};
- cout<<isSubSum(arr, 5, 9);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement