Advertisement
unknown_0711

Untitled

Nov 15th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. static int countSubarray(int arr[],
  2. int n, int k)
  3. {
  4. int start = 0, end = 0;
  5. int count = 0, sum = arr[0];
  6.  
  7. while (start < n && end < n) {
  8.  
  9. if (sum < k) {
  10. end++;
  11.  
  12. if (end >= start)
  13. count += end - start;
  14.  
  15. if (end < n)
  16. sum += arr[end];
  17. }
  18.  
  19. else {
  20. sum -= arr[start];
  21. start++;
  22. }
  23. }
  24.  
  25. return count;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement