Longest subarray

5

1 votes
Problem

Given an array of size N, print the length of the longest subarray such that the Bitwise OR of all the elements of the subarray is less than or equal to K.

If no such subarray exists, print 0.

A subbarray is a contiguous part of array. Consider the array [1, 2, 3, 4], the subarrays are (1), (2), (3), (4), (1,2), (2,3), (3,4), (1,2,3), (2,3,4) and (1,2,3,4) while (1, 3), (1,2,4) etc are not its subarrays.

Input:

  • First line contains an integer T, the number of test cases.
  • Each test contains two integer N and K, the size of the array and the value of K respectively.
  • Next line contains N space seperated integers denoting the elements of the array.

Output:

  • For each test case print the length of the longest valid subarray in a new line.

Constraints:

Subtasks:

Subtask 1 (20 points)

Subtask 2 (30 points)

Subtask 3 (50 points)

Sample Input
2
10 9
1 5 3 2 7 8 1 7 6 10
5 12
12 12 12 12 12 
Sample Output
5
5
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the first test case the longest subarray is [1 5 3 2 7] (Bitwise OR = 7)

In the second case the longest subarray is [12 12 12 12 12] (Bitwise OR = 12)

Editor Image

?