Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. bool isSubSum (int arr[], int n, int sum) {
  2.     int curr_sum = arr[0], s = 0;
  3.    
  4.     for (int e = 1; e <= n; e++) {
  5.         while (curr_sum > sum && s < e-1) {
  6.             curr_sum -= arr[s];
  7.             s++;
  8.         }
  9.        
  10.         if (curr_sum == sum) {
  11.             return true;
  12.         }
  13.        
  14.         if (e < n) {
  15.             curr_sum = arr[e];
  16.         }
  17.     }
  18.    
  19.     return (curr_sum == sum ? true : false);
  20. }
  21.  
  22. int main() {
  23.     int arr[]{7,8,2,4,5};
  24.     cout<<isSubSum(arr, 5, 9);
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement