Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
712 B
2
Indexable
Never
#include <iostream>
#include <vector>

using namespace std;

int numberOfProducts(int n, int x, vector<int>& arr) {
    int items = 0;
    int maxItems = 0;
    int totalCost = 0;
    int left = 0;

    for (int right = 0; right < n; right++) {
        totalCost += arr[right];

        while (totalCost > x) {
            totalCost -= arr[left];
            left++;
        }

        int itemsInRange = right - left + 1;
        if (itemsInRange > maxItems) {
            maxItems = itemsInRange;
        }
    }

    return maxItems;
}

int main() {
    int n, x;
    cin >> n >> x;
    vector<int> arr(n);

    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    int result = numberOfProducts(n,