Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
707 B
1
Indexable
Never
#include <iostream>
#include <vector>
#include <climits>

int minSubarrayLen(int target, std::vector<int>& nums) {
    int minLength = INT_MAX;
    int left = 0;
    int sum = 0;

    for (int right = 0; right < nums.size(); right++) {
        sum += nums[right];

        while (sum >= target) {
            minLength = std::min(minLength, right - left + 1);
            sum -= nums[left];
            left++;
        }
    }

    return (minLength != INT_MAX) ? minLength : 0;
}

int main() {
    int target = 7;
    std::vector<int> nums = {2, 3, 1, 2, 4, 3};

    int minLength = minSubarrayLen(target, nums);

    std::cout << "Minimal length of subarray: " << minLength << std::endl;

    return 0;
}