Minimum Impossible OR — LeetCode 2568 Python Solution
- Problem
- #2568
- Pattern
- Bit Manipulation
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given a 0-indexed integer array nums. We say that an integer x is expressible from nums if there exist some integers 0 <= index1 < index2 < ...
Example
- Input
- nums = [2,1]
- Output
- 4
- Explanation
- 1 and 2 are already present in the array. We know that 3 is expressible, since nums[0] | nums[1] = 2 | 1 = 3. Since 4 is not expressible, we return 4.
Python solution
class Solution:
def minImpossibleOR(self, nums: List[int]) -> int:
s = set(nums)
return next(1 << i for i in range(32) if 1 << i not in s)Complexity
| Measure | Complexity |
|---|---|
| Time | O(n + \log M) |
| Space | O(n) auxiliary |
Pattern: Bit Manipulation
Use XOR, masks and the low-bit trick to replace whole data structures with an integer. LeetCode 2568. Minimum Impossible OR is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Bit Manipulation.
The bit manipulation guide has the Python template for the pattern and the 194 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2568. Minimum Impossible OR?
- LeetCode 2568. Minimum Impossible OR is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2568. Minimum Impossible OR?
- The Python solution on this page runs in O(n + \log M).
- What is the space complexity of LeetCode 2568. Minimum Impossible OR?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 2568. Minimum Impossible OR cover?
- LeetCode 2568. Minimum Impossible OR is tagged Bit Manipulation, Brainteaser and Array on LeetCode.