Minimum Impossible OR - Problem
Minimum Impossible OR
You are given a 0-indexed integer array
An integer
In other words, an integer is expressible if it can be written as the bitwise OR of some subsequence of
Goal: Return the minimum positive non-zero integer that is not expressible from
Example: If
You are given a 0-indexed integer array
nums. Your task is to find the smallest positive integer that cannot be expressed as a bitwise OR of any subsequence from the array.An integer
x is expressible from nums if there exist indices 0 โค index1 < index2 < ... < indexk < nums.length such that:nums[index1] | nums[index2] | ... | nums[indexk] = xIn other words, an integer is expressible if it can be written as the bitwise OR of some subsequence of
nums.Goal: Return the minimum positive non-zero integer that is not expressible from
nums.Example: If
nums = [1, 2], we can express 1 (nums[0]), 2 (nums[1]), and 3 (nums[0] | nums[1]). The first number we cannot express is 4. Input & Output
example_1.py โ Basic Case
$
Input:
nums = [1, 2]
โบ
Output:
4
๐ก Note:
We can express 1 (from nums[0]), 2 (from nums[1]), and 3 (from nums[0] | nums[1] = 1 | 2 = 3). The number 4 cannot be expressed since neither element has the 3rd bit (value 4) set.
example_2.py โ Missing Power of 2
$
Input:
nums = [2, 6, 4]
โบ
Output:
1
๐ก Note:
None of the elements contain bit 0 (all are even numbers). Therefore, 1 cannot be expressed as an OR of any subsequence. We can express 2, 4, 6 individually and their combinations give us other even numbers.
example_3.py โ Single Element
$
Input:
nums = [8]
โบ
Output:
1
๐ก Note:
We only have one element: 8 (which is 1000โ). We cannot express any power of 2 smaller than 8, so the answer is 1, the smallest power of 2 not present.
Visualization
Tap to expand
Understanding the Visualization
1
Analyze Bit Patterns
Look at the binary representation of each number to see which powers of 2 are available
2
Check Powers Sequentially
Starting from 1, check each power of 2 to see if it exists in any array element
3
Find First Missing
The first power of 2 that doesn't appear in any element is our answer
Key Takeaway
๐ฏ Key Insight: The minimum impossible OR is always the smallest power of 2 (1, 2, 4, 8, 16, ...) that doesn't appear as a bit in any array element. This transforms a potentially exponential problem into a simple O(n log C) solution!
Time & Space Complexity
Time Complexity
O(n log C)
For each power of 2 up to max value C, we check all n elements
โก Linearithmic
Space Complexity
O(1)
Only uses a constant amount of extra space
โ Linear Space
Constraints
- 1 โค nums.length โค 105
- 1 โค nums[i] โค 109
- All elements in nums are distinct
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code