LeetCode 2568: Minimum Impossible OR Solution

Master LeetCode problem 2568 (Minimum Impossible OR), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2568. Minimum Impossible OR

Problem Explanation

Explanation

To solve this problem, we need to find the smallest positive integer that cannot be expressed as a bitwise OR of any subsequence of the given array nums. We can achieve this by iteratively updating the target value that we want to check for expressibility while iterating over the elements of the array. At each step, we check if the current element can help us construct the target value. If it can, we update the target value accordingly.

The algorithmic idea involves iterating over the elements of the array and using dynamic programming to keep track of the possible values that can be expressed. We initialize a reachable set with 0, and for each element in nums, we update the reachable values by OR-ing the current element with the values in the reachable set.

Finally, we iterate over the positive integers starting from 1 until we find a value that is not in the reachable set, which will be our answer.

  • Time complexity: O(N * log(MAX)), where N is the number of elements in nums and MAX is the maximum value in nums.
  • Space complexity: O(N).

Solution Code

import java.util.*;

class Solution {
    public int minIntegerImpossibleOR(int[] nums) {
        Set<Integer> reachable = new HashSet<>();
        reachable.add(0);
        
        for (int num : nums) {
            Set<Integer> newReachable = new HashSet<>(reachable);
            for (int val : reachable) {
                newReachable.add(val | num);
            }
            reachable = newReachable;
        }
        
        int target = 1;
        while (reachable.contains(target)) {
            target++;
        }
        
        return target;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2568 (Minimum Impossible OR)?

This page provides optimized solutions for LeetCode problem 2568 (Minimum Impossible OR) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2568 (Minimum Impossible OR)?

The time complexity for LeetCode 2568 (Minimum Impossible OR) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2568 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2568 in Java, C++, or Python.

Back to LeetCode Solutions