Talentd
FleetCode

3095. Shortest Subarray With OR at Least K I

Easy44.9% acceptance1 company

You are given an array nums of non-negative integers and an integer k.

An array is called special if the bitwise OR of all of its elements is at least k.

Return the length of the shortest special non-empty subarray of nums, or return -1 if no special subarray exists.

Example 1:

Input: nums = [1,2,3], k = 2

Output: 1

Explanation:

The subarray [3] has OR value of 3. Hence, we return 1.

Note that [2] is also a special subarray.

Example 2:

Input: nums = [2,1,8], k = 10

Output: 3

Explanation:

The subarray [2,1,8] has OR value of 11. Hence, we return 3.

Example 3:

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

Output: 1

Explanation:

The subarray [1] has OR value of 1. Hence, we return 1.

Constraints:

  • 1 <= nums.length <= 50
  • 0 <= nums[i] <= 50
  • 0 <= k < 64

Asked at 1 company

Hints open one at a time

Hint 1
The constraints are small. Brute force checking all the subarrays.
18:55
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editorโ€ฆ
1class Solution:
2 def minimumSubarrayLength(self, nums: List[int], k: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3]
2
Input
[2,1,8]
10
Input
[1,2]
0
Your code is saved on this device.Solution