The Indian Engineer

Problem 3097 Shortest Subarray With OR at Least K II

Posted on 5 mins

Array Bit-Manipulation Sliding Window

Problem Statement

Link - Problem 3097

Question

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.

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:

Solution

class Solution {
public:
    int minimumSubarrayLength(vector<int>& nums, int k) {
        vector<int> bit_count(32, 0);
        int current_val = 0;
        int start = 0, end = 0, min_length = INT_MAX;

        while (end < nums.size()) {
            update_bits(bit_count, current_val, nums[end], 1);

            while (start <= end && current_val >= k) {
                min_length = min(min_length, end - start + 1);
                update_bits(bit_count, current_val, nums[start], -1);
                start++;
            }

            end++;
        }

        return min_length == INT_MAX ? -1 : min_length;
    }

private:
    void update_bits(vector<int>& bit_count, int& current_val, int num, int val) {
        for (int i = 0; i < 32; i++) {
            if (num & (1 << i)) {
                bit_count[i] += val;

                // Update current_val based on the change in bit count
                if (bit_count[i] == 1 && val == 1) {
                    current_val |= (1 << i);  // Set the bit if it's newly active
                }
                else if (bit_count[i] == 0 && val == -1) {
                    current_val &= ~(1 << i); // Clear the bit if it's no longer active
                }
            }
        }
    }
};

Complexity Analysis

| Algorithm                        | Time Complexity | Space Complexity |
| -------------------------------- | --------------- | ---------------- |
| Sliding window with two-pointers | O(n)            | O(1)             |

Explanation

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

For each nums[i], we can maintain each subarray’s bitwise OR result ending with it.

The property of bitwise OR is that it never unsets any bits and only sets new bits

So the number of different results for each nums[i] is at most the number of bits 32.


Similar Questions:

Title URL Difficulty
Maximum Size Subarray Sum Equals k https://leetcode.com/problems/maximum-size-subarray-sum-equals-k Medium
Shortest Subarray with Sum at Least K https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k Hard