Shortest Subarray With OR at Least K II

Medium Topics Company Tags

Return the minimum length of a nonempty contiguous subarray whose bitwise OR is at least k. If no such subarray exists, return -1. The bitwise OR has a bit set whenever at least one number in the subarray has that bit set.

Example 1:

Input: nums = [1,4,2]
k = 7

Output: 3

Explanation: The OR of all three elements is 7. Every shorter contiguous subarray has OR below 7, so the minimum length is 3.

Example 2:

Input: nums = [0,0]
k = 1

Output: -1

Explanation: Any subarray consists entirely of zeros, whose OR is zero. None reaches 1.

Example 3:

Input: nums = [0]
k = 0

Output: 1

Explanation: The single zero has OR equal to the target 0. A nonempty subarray must contain at least one element, so the answer is 1.

Constraints:

  • 1 <= nums.length <= 200000
  • 0 <= nums[i] <= 1000000000
  • 0 <= k <= 1000000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.