Skip to content
Katabench
Try free

Minimum Size Subarray Sum

Medium About 20 min Free, no account needed

The shortest window of positive numbers reaching a target sum.

The brief

Given an array of positive integers nums and a positive target, return the length of the shortest contiguous subarray whose sum is at least target, or 0 if none exists.

The performance bar

The obvious approach grows a sum from every starting index, O(n2) when the qualifying windows are long. The hidden test needs windows of 100,000 elements out of 200,000, so the quadratic version is too slow. You will need an approach where the work stays close to O(n).

Starter code

C#
using System;

public class Solution
{
    // TODO: return the length of the shortest contiguous subarray summing to at least target (0 if none).
    public int MinSubarrayLen(int[] nums, int target)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

What it practices

Sliding window
Grow and shrink a contiguous range while maintaining its state incrementally.

Related katas