A fleet of n relay drones, numbered 0 to n-1, each continuously broadcasts one non-negative integer signal code, given as code[0], code[1], ..., code[n-1]. When a contiguous run of drones combines their broadcasts, the fleet's combined code for that run is the bitwise OR of every code in it. Mission control needs a contiguous run whose combined code is at least a required threshold k (compared as ordinary integers).
Find the length of the shortest contiguous run of drones whose combined code is >= k. If no contiguous run achieves this, output 0.
Line 1: two integers n and k, separated by a space.
Line 2: n integers code[0], code[1], ..., code[n-1], separated by spaces.
A single integer: the length of the shortest qualifying contiguous run, or 0 if none qualifies.
1 <= n <= 500 <= code[i] < 2^30 for every i0 <= k < 2^30Example 1
Input
5 7 1 2 4 1 1
Expected
3
Explanation
code = [1,2,4,1,1], k=7. No single value reaches 7 (the largest is 4), and no length-2 window reaches 7 either (1|2=3, 2|4=6, 4|1=5, 1|1=1). The length-3 window [1,2,4] (indices 0-2) has combined code 1|2|4=7, which meets the threshold, so the shortest qualifying run has length 3.
Example 2
Input
3 100 1 2 4
Expected
0
Explanation
code=[1,2,4], k=100. Even the OR of the entire array is only 1|2|4=7, which never reaches 100, so no contiguous run qualifies and the answer is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →