LeetCode-in-Java

3097. Shortest Subarray With OR at Least K II

Medium

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

public class Solution {
    public int minimumSubarrayLength(int[] nums, int k) {
        int n = nums.length;
        if (nums[0] >= k) {
            return 1;
        }
        int res = Integer.MAX_VALUE;
        for (int i = 1; i < n; i++) {
            if (nums[i] >= k) {
                return 1;
            }
            for (int j = i - 1; j >= 0 && (nums[i] | nums[j]) != nums[j]; j--) {
                nums[j] |= nums[i];
                if (nums[j] >= k) {
                    res = Math.min(res, i - j + 1);
                }
            }
        }
        if (res == Integer.MAX_VALUE) {
            return -1;
        }
        return res;
    }
}