#C8103. Minimum Subarray Length with Sum At Least K

    ID: 52049 Type: Default 1000ms 256MiB

Minimum Subarray Length with Sum At Least K

Minimum Subarray Length with Sum At Least K

Given an array of integers and an integer \(k\), your task is to determine the minimum length of a contiguous subarray whose sum is greater than or equal to \(k\). If no such subarray exists, output \(-1\). This problem can be efficiently solved using a sliding window approach.

Note: A contiguous subarray is a sequence of elements that are consecutive in the original array.

inputFormat

The input begins with an integer \(T\) denoting the number of test cases. Each test case consists of two lines:

  1. The first line contains two integers \(n\) and \(k\), where \(n\) is the number of elements in the array and \(k\) is the target sum.
  2. The second line contains \(n\) space-separated integers representing the array. If \(n = 0\), this line will be empty.

outputFormat

For each test case, output a single integer on a new line representing the length of the smallest contiguous subarray whose sum is at least \(k\). If no such subarray exists, output \(-1\).

## sample
6
6 7
2 1 5 2 3 2
5 7
2 1 5 2 8
5 8
3 4 1 1 6
5 16
1 2 3 4 5
5 5
1 1 1 1 1
0 5
2

1 3 -1 5 -1

</p>