Advertisement
nikunjsoni

930

Apr 10th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int atMost(vector<int> & A, int S){
  4.         int ans, left, right;
  5.         ans = 0;
  6.         if(S < 0) return 0;
  7.         for(left=right=0; right<A.size(); right++){
  8.             S -= A[right];
  9.             while(S < 0)
  10.                 S += A[left++];
  11.             ans += right-left+1;
  12.         }
  13.         return ans;
  14.     }
  15.     int numSubarraysWithSum(vector<int>& A, int S) {        
  16.         return atMost(A, S) - atMost(A, S-1);
  17.     }
  18. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement