Advertisement

Shortest Subarray With OR at Least K II - LeetCode 3097 Solution

Shortest Subarray With OR at Least K II - Complete Solution Guide

Shortest Subarray With OR at Least K II is LeetCode problem 3097, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 . H

Detailed Explanation

The problem requires us to find the shortest subarray within a given array `nums` of non-negative integers whose bitwise OR is at least `k`. We need to return the length of this shortest subarray, or -1 if no such subarray exists. A subarray is considered 'special' if the bitwise OR of all its elements is greater than or equal to `k`. The constraints specify the size of `nums` and the range of values it contains.

Solution Approach

The solution uses a sliding window approach with two pointers, `left` and `right`, to define the current subarray. It maintains an array `bit_counts` to store the count of each bit (0 to 30) present in the current window. The `current_or` variable tracks the bitwise OR of the elements within the current window. As the right pointer moves through the array, the corresponding bits of the new element are added to the `bit_counts`. If a bit count becomes 1 from 0, it means this bit is added to the `current_or`. Then, the left pointer moves to shrink the window as long as the `current_or` remains greater than or equal to `k`, updating `bit_counts` and `current_or` accordingly. During the window shrinking process, the minimum length is tracked.

Step-by-Step Algorithm

  1. Step 1: Initialize `left` to 0, `min_length` to infinity, `current_or` to 0, and `bit_counts` to an array of 31 zeros.
  2. Step 2: Iterate through the array `nums` using the `right` pointer (sliding window's right boundary).
  3. Step 3: For each element at index `right`, update the `bit_counts` array by incrementing the count of each bit present in the element. If adding the element sets a bit to 1 for the first time in the window, update the `current_or` accordingly.
  4. Step 4: While the `current_or` is greater than or equal to `k` and `left` is less than or equal to `right`, update `min_length` with the minimum between the current `min_length` and the length of the current window (`right - left + 1`).
  5. Step 5: Shrink the window from the left by decrementing the `bit_counts` for the element at index `left`. If removing the element causes a bit to become 0 from 1 in the window, clear this bit in the `current_or` as well.
  6. Step 6: Increment the `left` pointer to move the window's left boundary.
  7. Step 7: After the loop finishes, return `min_length` if it was updated (i.e., not infinity), otherwise return -1.

Key Insights

  • Insight 1: The problem can be efficiently solved using the sliding window technique due to the contiguous subarray requirement.
  • Insight 2: Bitwise OR operation is monotonic, i.e., adding more elements to a subarray can only increase its OR value (or keep it the same).
  • Insight 3: Maintaining a count of the occurrences of each bit within the sliding window helps track the current OR value and determine when to shrink the window.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Bit Manipulation, Sliding Window.

Companies

Asked at: Mitsogo.