Advertisement
cosenza987

Untitled

Jul 17th, 2021
1,344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios_base::sync_with_stdio(false);
  7.     cin.tie(0);
  8.     int n, k;
  9.     cin >> n >> k;
  10.     int arr[n];
  11.     for(int i = 0; i < n; i++) {
  12.         cin >> arr[i];
  13.     }
  14.     long long ans = 0;
  15.     for(int l = 0; l < n; l++) {
  16.         int sum = arr[l];
  17.         if(sum > k) {
  18.             continue;
  19.         }
  20.         for(int r = l; r < n; r++) {
  21.             if(l == r) {
  22.                 if(sum == k) {
  23.                     ans++;
  24.                 }
  25.                 continue;
  26.             } else {
  27.                 sum += arr[r];
  28.             }
  29.             if(sum > k) {
  30.                 break;
  31.             } else if(sum == k) {
  32.                 ans++;
  33.             }
  34.         }
  35.     }
  36.     cout << ans << "\n";
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement