diff --git a/3097. Shortest Subarray With OR at Least K II b/3097. Shortest Subarray With OR at Least K II new file mode 100644 index 0000000..3b063e6 --- /dev/null +++ b/3097. Shortest Subarray With OR at Least K II @@ -0,0 +1,53 @@ +class Solution { +public: + int minimumSubarrayLength(vector& nums, int k) { + + int n = nums.size(); + int z = *max_element(nums.begin(), nums.end()); + + if(z >= k) + return 1; + + int ans = INT_MAX; + int x = nums[0]; + int i = 0; + int j = 1; + + while(j < n) + { + x |= nums[j]; + if(x < k) + { + j++; + } + else + { + ans = min(ans, j - i + 1); + while(i < n && i <= j && x >= k) + { + ans = min(ans, j - i + 1); + + if(nums[i] == nums[i+1]) + { + i++; + } + else + { + i++; + x = nums[i]; + for(int t = i + 1; t <= j; t++) + { + x |= nums[t]; + } + } + } + j++; + } + } + + if(ans == INT_MAX) + return -1; + + return ans; + } +};