Skip to contentKEEP THE CRAFTTHE SANCTUARY

Shortest Subarray With OR at Least K

Find the shortest subarray whose bitwise OR is at least k.

The problem

Implement Solution.minimumSubarrayLength(nums, k). nums holds non-negative integers. Return the length of the shortest non-empty contiguous subarray whose bitwise OR of all its values is at least k, or -1 if there is none. For example, nums = [2, 1, 8] with k = 10 gives 3, because 2 | 1 | 8 = 11 and no shorter subarray reaches 10. nums has between 1 and 200,000 values, each from 0 to 1,000,000,000, and 0 <= k < 2^30.

Starter code

class Solution:
    def minimumSubarrayLength(self, nums, k):
        pass

Public examples

  1. nums = [1, 2, 3], k = 2gives1
  2. nums = [0, 0], k = 0gives1
  3. nums = [1, 1, 2], k = 3gives2

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.