#K88392. Minimum Length Subarray with Product Constraint

    ID: 37298 Type: Default 1000ms 256MiB

Minimum Length Subarray with Product Constraint

Minimum Length Subarray with Product Constraint

You are given an array of positive integers and a target integer (K). Your task is to find the minimum length of a contiguous subarray such that the product of its elements is greater than or equal to (K). If no such subarray exists, output (-1).

For instance, given the array [1, 2, 3, 4] and (K=10), the subarray [3, 4] has a product of 12 which is (\ge K) and its length is 2, which is the minimum possible in this case.

Hint: You may find the sliding window technique useful for an optimal solution. The problem can be mathematically represented as finding the smallest (l) such that (\prod_{i=s}^{s+l-1} a_i \ge K) for some starting index (s).

inputFormat

The input is given via standard input (stdin). The first line contains an integer (T), the number of test cases. Each test case consists of two lines. The first line contains two integers (N) and (K), where (N) is the number of elements in the array and (K) is the target product. The second line contains (N) positive integers representing the array.

outputFormat

For each test case, output a single line containing the minimum length of the contiguous subarray with product at least (K). If no such subarray exists, output (-1). The output should be printed to standard output (stdout).## sample

1
4 10
1 2 3 4
2

</p>