fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void executeTime() {
  5. cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " secs";
  6. }
  7.  
  8. int main() {
  9.  
  10.  
  11. int n, k; cin >> n >> k;
  12.  
  13. vector<int> v(n);
  14.  
  15. for (auto &x : v) {
  16. cin >> x;
  17. }
  18.  
  19. int p = 0;
  20. unordered_map<int, int> mp;
  21. mp[0] = 0;
  22.  
  23. int ans = -1;
  24.  
  25. for (int i = 0; i < n; i++) {
  26. p += v[i];
  27.  
  28. if (mp.count(p - k)) {
  29. ans = max(ans, i + 1 - mp[p - k]);
  30. }
  31.  
  32. if (!mp.count(p)) mp[p] = i + 1;
  33. }
  34.  
  35. cout << ans << endl;
  36.  
  37. executeTime();
  38. return 0;
  39. }
Success #stdin #stdout #stderr 0.01s 5284KB
stdin
10 5
5 0 4 1 50
4 32 2 4 50
stdout
3
stderr
Time Taken: 0.007584 secs