#K11806. Smallest Subarray with Sum Greater Than or Equal to T

    ID: 23550 Type: Default 1000ms 256MiB

Smallest Subarray with Sum Greater Than or Equal to T

Smallest Subarray with Sum Greater Than or Equal to T

Given an array of positive integers and a target integer \(T\), find the smallest contiguous subarray whose sum is greater than or equal to \(T\). If no such subarray exists, output -1.

For example, if the input array is [2, 3, 1, 2, 4, 3] and \(T = 7\), the smallest subarray with sum \(\geq 7\) is [4, 3] because \(4 + 3 = 7\) and no shorter subarray meets the condition.

This problem can be efficiently solved using the sliding window technique in \(O(n)\) time.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  • The first line contains two integers \(n\) and \(T\), where \(n\) is the number of elements in the array and \(T\) is the target sum.
  • The second line contains \(n\) space-separated positive integers representing the array.

outputFormat

Print a single line to standard output (stdout):

  • If a valid subarray exists, output its elements separated by a single space.
  • If there is no subarray whose sum is at least \(T\), output -1.
## sample
6 7
2 3 1 2 4 3
4 3