Advertisement
Josif_tepe

Untitled

Jan 27th, 2023
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n, M;
  7.     cin >> n >> M;
  8.    
  9.     int niza[n];
  10.     for(int i = 0; i < n; i++) {
  11.         cin >> niza[i];
  12.     }
  13.    
  14.     int j = 0;
  15.     int sum = 0;
  16.     int rezultat = 0;
  17.     for(int i = 0; i < n; i++) {
  18.         while(j < n and sum + niza[j] <= M) {
  19.             sum +=  niza[j];
  20.             j++;
  21.         }
  22.         if(rezultat < j - i) {
  23.             rezultat = j - i;
  24.         }
  25.         sum -= niza[i];
  26.     }
  27.     cout << rezultat << endl;
  28.     return 0;
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement