#C11569. Shortest Subarray with Bitwise AND Condition

    ID: 40899 Type: Default 1000ms 256MiB

Shortest Subarray with Bitwise AND Condition

Shortest Subarray with Bitwise AND Condition

You are given T test cases. In each test case, you are given two integers N and K and an array A of N integers. Your task is to determine the length of the shortest contiguous subarray such that the bitwise AND of all its elements is at least K.

If there is no such subarray, output -1. The bitwise AND operator, denoted by \(\land\), is used for combining the bits of two numbers. Formally, for any subarray spanning indices \(i\) to \(j\), you need to check if \[ A_i \land A_{i+1} \land \cdots \land A_j \ge K \] If the inequality is satisfied, a valid subarray is found.

Note: A subarray is a contiguous segment of the array.

inputFormat

The input is read from standard input (stdin) and has the following format:

T
N K
A[0] A[1] ... A[N-1]
... (repeated T times)

Where:

  • T is the number of test cases.
  • For each test case, the first line contains two space-separated integers, N and K.
  • The second line contains N space-separated integers representing the array A.
  • </p>

    outputFormat

    For each test case, output a single line to standard output (stdout) containing the length of the shortest contiguous subarray such that the bitwise AND of all its elements is at least K. If no valid subarray exists, output -1.

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

    -1 1 1

    </p>