#C7427. Minimum Subarray Length

    ID: 51297 Type: Default 1000ms 256MiB

Minimum Subarray Length

Minimum Subarray Length

You are given an array of positive integers and an integer \( k \). Your task is to find the length of the smallest contiguous subarray of which the sum is greater than or equal to \( k \). If there is no such subarray, output \(-1\).

Problem Details:

  • You are given \( T \) test cases.
  • For each test case, the first input line contains two integers: \( n \) (the length of the array) and \( k \) (the target sum).
  • The second line contains \( n \) space-separated positive integers representing the array elements.
  • For each test case, you need to output the minimum length of any contiguous subarray whose sum is at least \( k \). If no such subarray exists, output \(-1\).

This problem can be efficiently solved using the sliding window technique.

inputFormat

The first line of input contains 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.
  2. The second line contains \( n \) space-separated positive integers representing the array.

All input is to be read from standard input (stdin).

outputFormat

For each test case, output a single line containing one integer: the minimum length of a contiguous subarray with sum at least \( k \), or \(-1\) if no such subarray exists. Output each result on its own line to standard output (stdout).

## sample
2
5 11
1 2 3 4 5
4 15
1 2 3 4
3

-1

</p>