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 minimumpositive non-zero integer that is not _expressible fromnums.
Input: nums =[2,1]Output: 4Explanation: 1 and 2 are already present in the array. We know that 3is expressible, since nums[0]| nums[1]=2|1=3. Since 4is not expressible, we return4.
The only way a positive integer cannot be expressed as the OR of a subset is if it is a power of two that cannot be formed from the available numbers. If all powers of two up to a certain value can be formed, the next missing power of two is the answer.
classSolution {
public:int minImpossibleOR(vector<int>& nums) {
unordered_set<int> s(nums.begin(), nums.end());
int x =1;
while (s.count(x)) x <<=1;
return x;
}
};
classSolution {
publicintminImpossibleOR(int[] nums) {
Set<Integer> s =new HashSet<>();
for (int x : nums) s.add(x);
int x = 1;
while (s.contains(x)) x <<= 1;
return x;
}
}
1
2
3
4
5
6
7
8
classSolution {
funminImpossibleOR(nums: IntArray): Int {
val s = nums.toSet()
var x = 1while (x in s) x = x shl 1return x
}
}
1
2
3
4
5
6
defmin_impossible_or(nums: list[int]) -> int:
s = set(nums)
x =1while x in s:
x <<=1return x
1
2
3
4
5
6
7
8
9
10
impl Solution {
pubfnmin_impossible_or(nums: Vec<i32>) -> i32 {
let s: std::collections::HashSet<_>= nums.into_iter().collect();
letmut x =1;
while s.contains(&x) {
x <<=1;
}
x
}
}