> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Shortest Subarray With OR at Least K II

> Tested Python solution for LeetCode 3097 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 3097, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Bit Manipulation](/catalog/topics/bit-manipulation), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-ii/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 3097   # by problem number
lcpy gen -s shortest_subarray_with_or_at_least_k_ii   # by problem name
```

## Problem

You are given an array \<code>nums\</code> of \<strong>non-negative\</strong> integers and an integer \<code>k\</code>.\</p>

\<p>An array is called \<strong>special\</strong> if the bitwise \<code>OR\</code> of all of its elements is \<strong>at least\</strong> \<code>k\</code>.\</p>

\<p>Return \<em>the length of the \<strong>shortest\</strong> \<strong>special\</strong> \<strong>non-empty\</strong> \<span data-keyword="subarray-nonempty">subarray\</span> of\</em> \<code>nums\</code>, \<em>or return\</em> \<code>-1\</code> \<em>if no special subarray exists\</em>.\</p>

### Examples

```
Input: nums = [1,2,3], k = 2
Output: 1
Explanation:
The subarray [3] has OR value of 3. Hence, we return 1.
```

```
Input: nums = [2,1,8], k = 10
Output: 3
Explanation:
The subarray [2,1,8] has OR value of 11. Hence, we return 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 \<= 2 \* 10^5
* 0 \<= nums\[i] \<= 10^9
* 0 \<= k \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/shortest_subarray_with_or_at_least_k_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/shortest_subarray_with_or_at_least_k_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * 30)
    # Space: O(30)
    def minimum_subarray_length(self, nums: list[int], k: int) -> int:
        best = len(nums) + 1
        counts = [0] * 30
        left = 0
        for right, value in enumerate(nums):
            for bit in range(30):
                if (value >> bit) & 1:
                    counts[bit] += 1
            while left <= right and self._or_value(counts) >= k:
                best = min(best, right - left + 1)
                for bit in range(30):
                    if (nums[left] >> bit) & 1:
                        counts[bit] -= 1
                left += 1
        return -1 if best == len(nums) + 1 else best

    def _or_value(self, counts: list[int]) -> int:
        value = 0
        for bit, count in enumerate(counts):
            if count:
                value |= 1 << bit
        return value
```

## Complexity

| Time | Space |
| - | - |
| O(n \* 30) | O(30) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.