Leetcode 2568: Minimum Impossible OR

grid47
grid47
Exploring patterns and algorithms
Feb 24, 2024 5 min read

You are given a 0-indexed integer array nums. A number x is expressible from nums if there exists a subsequence of elements in nums whose bitwise OR equals x. Your task is to return the smallest positive integer that cannot be expressed as the bitwise OR of any subsequence from nums.
Problem
Approach
Steps
Complexity
Input: The input consists of an integer array nums, where each element represents a number.
Example: For example, nums = [1, 2, 4].
Constraints:
• 1 <= nums.length <= 10^5
• 1 <= nums[i] <= 10^9
Output: Return the smallest positive integer that cannot be expressed as the bitwise OR of any subsequence from nums.
Example: For nums = [1, 2, 4], the output is 8.
Constraints:
• The result must be a positive integer that cannot be formed by the OR operation of any subsequence of the input array.
Goal: To find the smallest positive integer that cannot be expressed as the bitwise OR of any subsequence from nums.
Steps:
• 1. Initialize a set to store the unique OR values of subsequences.
• 2. Traverse the nums array and compute the bitwise OR for all possible subsequences.
• 3. Check which positive integer is not part of the computed values, starting from 1 upwards.
Goal: The length of the nums array is between 1 and 100,000, and each integer in nums lies between 1 and 10^9.
Steps:
• 1 <= nums.length <= 10^5
• 1 <= nums[i] <= 10^9
Assumptions:
• The nums array will always contain at least one element.
• The integers in nums are positive and within the specified range.
Input: For nums = [1, 2, 4], we can express 1, 2, 3 (from 1 | 2), 4, 5 (from 1 | 4), 6 (from 2 | 4), and 7 (from 1 | 2 | 4).
Explanation: The smallest integer that cannot be expressed is 8.

Input: For nums = [7, 3, 1], we can express 1, 3, 7, 4 (from 1 | 3), and 6 (from 3 | 7).
Explanation: The smallest integer that cannot be expressed is 8.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus