Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions 3097. Shortest Subarray With OR at Least K II
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Solution {
public:
int minimumSubarrayLength(vector<int>& 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;
}
};
Loading