#K45377. Minimum Contiguous Trees for Fruit Collection

    ID: 27740 Type: Default 1000ms 256MiB

Minimum Contiguous Trees for Fruit Collection

Minimum Contiguous Trees for Fruit Collection

You are given several test cases. In each test case, there is a row of N trees, each bearing a certain number of fruits. Your task is to determine the minimum number of contiguous trees you need to pick so that the sum of the fruits is at least a given value S. If it is impossible to reach a sum of S using any contiguous subarray of trees, output -1.

Formally, given an array a1, a2, ..., aN and an integer S, find the smallest integer l such that there exists an index i with \[ a_i + a_{i+1} + \cdots + a_{i+l-1} \geq S \] If no such l exists, output -1.

This problem can be solved using a sliding window (two-pointer) approach in O(N) time per test case.

inputFormat

The first line contains an integer T representing the number of test cases. For each test case:

  • The first line contains two integers: N (the number of trees) and S (the target sum of fruits).
  • The second line contains N space-separated integers representing the number of fruits on each tree.

outputFormat

For each test case, output a single line containing the minimum length of the contiguous segment whose sum of fruits is at least S. If no such segment exists, output -1.

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

5

</p>