LeetCode solutions

3611. Construct the Minimum Bitwise Array II

My accepted Python solution to LeetCode problem 3611, Construct the Minimum Bitwise Array II, running in 0ms.

  • Difficulty: Medium
  • Python
  • Runtime 0ms
  • Memory 17.6MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.6MB, accepted 2026-01-01.

python
class Solution:
    def minBitwiseArray(self, nums: List[int]) -> List[int]:
        result = []
        for num in nums:
            if num == 2:
                result.append(-1)
            else:
                # Find the lowest set bit position where there's a consecutive run of 1s
                # For x OR (x+1) = num, we need to find x
                # If num has trailing 1s, x will flip the lowest 0 bit
                
                # Find the position of the lowest 0 bit in num
                # x OR (x+1) = num means x+1 flips some bits in x to produce num when OR'd
                
                # For odd numbers > 2, we can work backwards
                # If num = ...1111 (binary), then ans = num - 1 doesn't work
                # We need to find lowest position where 0 appears and set ans accordingly
                
                # Find lowest 0 bit position
                pos = 0
                temp = num
                while temp & 1:
                    pos += 1
                    temp >>= 1
                
                # ans should be num with the bit at position (pos-1) turned off
                ans = num ^ (1 << (pos - 1))
                result.append(ans)
        
        return result

Source