Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 < ... < indexk < nums.length for which 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.
Return the minimum positive non-zero integer that is not expressible from nums.
Example 1:
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.
Example 2:
Input: nums = [5,3,2] Output: 1 Explanation: We can show that 1 is the smallest number that is not expressible.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109Problem summary: 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 < ... < indexk < nums.length for which 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. Return the minimum positive non-zero integer that is not expressible from nums.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[2,1]
[5,3,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2568: Minimum Impossible OR
class Solution {
public int minImpossibleOR(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int x : nums) {
s.add(x);
}
for (int i = 0;; ++i) {
if (!s.contains(1 << i)) {
return 1 << i;
}
}
}
}
// Accepted solution for LeetCode #2568: Minimum Impossible OR
func minImpossibleOR(nums []int) int {
s := map[int]bool{}
for _, x := range nums {
s[x] = true
}
for i := 0; ; i++ {
if !s[1<<i] {
return 1 << i
}
}
}
# Accepted solution for LeetCode #2568: Minimum Impossible OR
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)
// Accepted solution for LeetCode #2568: Minimum Impossible OR
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2568: Minimum Impossible OR
// class Solution {
// public int minImpossibleOR(int[] nums) {
// Set<Integer> s = new HashSet<>();
// for (int x : nums) {
// s.add(x);
// }
// for (int i = 0;; ++i) {
// if (!s.contains(1 << i)) {
// return 1 << i;
// }
// }
// }
// }
// Accepted solution for LeetCode #2568: Minimum Impossible OR
function minImpossibleOR(nums: number[]): number {
const s: Set<number> = new Set();
for (const x of nums) {
s.add(x);
}
for (let i = 0; ; ++i) {
if (!s.has(1 << i)) {
return 1 << i;
}
}
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.