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 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 Inputs
-
Example 1:
- Input: nums = [1, 2, 3], k = 2
- Output: 1
- Explanation: The subarray [3] has an OR value of 3, meeting the criteria. Thus, the answer is 1.
-
Example 2:
- Input: nums = [2, 1, 8], k = 10
- Output: 3
- Explanation: The subarray [2, 1, 8] has an OR value of 11, which is at least
k. Hence, the length is 3.
-
Example 3:
- Input: nums = [1, 2], k = 0
- Output: 1
- Explanation: The subarray [1] has an OR value of 1, which is greater than or equal to
k, so the answer is 1.
Brute Force Approach
a. Approach
For each subarray of nums, calculate the bitwise OR of its elements and check if it is at least k. Track the length of the shortest subarray that meets the condition.
b. Steps
- Loop over all starting indices of subarrays.
- For each start index, calculate the OR of all possible subarrays beginning there.
- Track the minimum length of subarrays with OR value at least
k.
c. Time & Space Complexity
- Time Complexity:
O(n^2) due to evaluating the OR for each subarray.
- Space Complexity:
O(1) since only a few variables are used.
d. Code Snippet
e. Dry Run
- Input: nums = [1, 2, 3], k = 2
- Starting from each element, calculate ORs and find the shortest valid subarray.
Efficient Approach
a. Approach (Sliding Window)
Using a sliding window approach, maintain a window with elements that meet the condition (OR >= k). Adjust the window as needed to track the shortest length.
b. Steps
- Use a window that expands by moving the
right pointer and includes each element into the current OR value.
- If the OR of the current window meets or exceeds
k, update the shortest length and increment left to try for a shorter subarray.
- Repeat until the end of the array.
c. Time & Space Complexity
- Time Complexity:
O(n) as each element is considered only once per window.
- Space Complexity:
O(1) as minimal additional storage is used.
d. Code Snippet
e. Dry Run
- Input: nums = [2, 1, 8], k = 10
- Using a sliding window, adjust the window to maintain the shortest length that meets the OR requirement.
Complexity Analysis
a. Brute Force Time & Space Complexity
- Time Complexity:
O(n^2)
- Space Complexity:
O(1)
b. Optimized Time and Space Complexity
- Time Complexity:
O(n)
- Space Complexity:
O(1)
Conclusion
The brute force approach checks each subarray individually, which is inefficient for larger arrays. The sliding window technique reduces complexity by dynamically maintaining a valid window, providing a faster solution.