Minimum Impossible OR — LeetCode 2568 Python Solution

MediumBit ManipulationBrainteaserArray
Problem
#2568
Reading time
2 min

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

Python
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

MeasureComplexity
TimeO(n + \log M)
SpaceO(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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview