algoadvance

Leetcode 3095. Shortest Subarray With OR at Least K I

Problem Statement

On Interviewbit, you have been given a problem where you need to find the shortest subarray from a given array such that the bitwise OR of the elements of this subarray is at least a given integer K.

Formally, given an array A of N non-negative integers and an integer K, you need to find the length of the shortest subarray such that the bitwise OR of all elements in that subarray is greater than or equal to K. If no such subarray exists, return -1.

Clarifying Questions

  1. Input constraints: What are the constraints on the values of N, elements of A, and K?
  2. Edge cases: Should we consider the possibility of having all elements of the array being zero or smaller than K?
  3. Output clarity: Should the function return the length (integer type) of the shortest subarray, or some additional information?

Assumptions and Constraints

  1. 1 <= N <= 10^5
  2. 0 <= A[i], K <= 10^9

Strategy

  1. Two Pointers Technique: We will iterate over the array with two pointers to maintain a sliding window of elements.
  2. Bitwise OR Calculation: As we slide the window, we will calculate the bitwise OR of the current subarray.
  3. Shortest Length: Track the minimum length of the subarrays whose OR value is at least K.

Code

#include <iostream>
#include <vector>
#include <climits>
using namespace std;

int shortestSubarrayWithORAtLeastK(const vector<int>& A, int K) {
    int n = A.size();
    int left = 0, right = 0;
    int current_or = 0;
    int min_length = INT_MAX;

    while (right < n) {
        // Extend the slide window to the right
        current_or |= A[right];

        // While current_or is at least K, try to shrink the window from the left
        while (left <= right && current_or >= K) {
            min_length = min(min_length, right - left + 1);
            current_or &= ~A[left];  // Remove the effect of A[left] from current_or
            left++;
        }

        // Increment right pointer to extend the window
        right++;
    }

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

int main() {
    vector<int> A = {1, 2, 4, 8};
    int K = 7;

    int result = shortestSubarrayWithORAtLeastK(A, K);
    cout << "The length of the shortest subarray: " << result << endl;

    return 0;
}

Strategy Explanation

  1. Initialization:
    • Start with two pointers, left and right, both set to the beginning of the array.
    • Use current_or to store the OR value of the current subarray.
    • Use min_length to keep track of the smallest window length found.
  2. Sliding Window:
    • Iterate with right to extend the window, updating the OR value by including A[right].
    • Once current_or reaches or exceeds K, determine if the current window is the smallest by comparing its length with min_length.
    • Increment left to shrink the window from the left. While doing this, exclude A[left] from the OR calculation by using bitwise operations.
  3. Result Check:
    • If no valid subarray was found, min_length remains unchanged and we return -1, otherwise return the smallest length found.

Time Complexity

This should provide an efficient solution to the problem statement.

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI