Minimum Impossible OR - Problem
Minimum Impossible OR

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] = x

In 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
Binary Building Blocks ConceptExample: nums = [2, 6, 4]2 = 010= 010โ‚‚6 = 011= 110โ‚‚4 = 001= 100โ‚‚Available Powers of 2:Power 1 (2โฐ): โŒ Missing in all numbersPower 2 (2ยน): โœ… Present in 2 and 6Power 4 (2ยฒ): โœ… Present in 6 and 4Result: 1Since power 1 is missing from all elements, we cannotcreate the number 1 using OR operations on any subsequence.๐Ÿ’ก Key InsightThe minimum impossible OR is always the smallest power of 2 not present in any array element!
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

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only uses a constant amount of extra space

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • All elements in nums are distinct
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
18.5K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen