#include <iostream>
#include <vector>
#include <climits>
using namespace std;

int minSubArrayLen(int target, vector<int>& nums) {
    int n = nums.size();

    int left = 0;
    int sum = 0;
    int minLength = INT_MAX;

    for (int right = 0; right < n; right++) {

        sum += nums[right];

        while (sum >= target) {

            minLength = min(minLength, right - left + 1);

            sum -= nums[left];
            left++;
        }
    }

    if (minLength == INT_MAX) {
        return 0;
    }

    return minLength;
}

int main() {
    int n, target;

    cin >> n >> target;

    vector<int> nums(n);

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

    cout << minSubArrayLen(target, nums) << endl;

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: