Check if Bitwise OR Has Trailing Zeros - Problem

You're given an array of positive integers, and your task is to determine if it's possible to select two or more elements such that their bitwise OR operation results in a number with at least one trailing zero.

A trailing zero in binary representation means the number ends with one or more '0' bits. For example:

  • 5 in binary is "101" - no trailing zeros
  • 4 in binary is "100" - two trailing zeros
  • 12 in binary is "1100" - two trailing zeros

Return true if such a selection is possible, false otherwise.

Key insight: A number has trailing zeros if and only if it's divisible by 2 (even). So we need to find if any combination of 2+ elements can produce an even OR result.

Input & Output

example_1.py โ€” Basic Case with Even Numbers
$ Input: [2, 1, 3]
โ€บ Output: false
๐Ÿ’ก Note: Only one even number (2) exists. We need at least 2 even numbers to create an OR result with trailing zeros. 2|1=3 (odd), 2|3=3 (odd), 1|3=3 (odd), 2|1|3=3 (odd).
example_2.py โ€” Multiple Even Numbers
$ Input: [1, 2, 3, 4, 5]
โ€บ Output: true
๐Ÿ’ก Note: We have two even numbers: 2 and 4. Their OR is 2|4 = 6 (binary: 110), which has one trailing zero. Since 6 is even, it has at least one trailing zero.
example_3.py โ€” All Odd Numbers
$ Input: [1, 3, 5, 7]
โ€บ Output: false
๐Ÿ’ก Note: All numbers are odd. Any OR combination of odd numbers will always result in an odd number, which cannot have trailing zeros.

Visualization

Tap to expand
Binary Patterns and OR OperationsOdd Numbers (end with 1):3 = 011โ‚‚5 = 101โ‚‚7 = 111โ‚‚Even Numbers (end with 0):2 = 010โ‚‚4 = 100โ‚‚8 = 1000โ‚‚โŒ Mixing Odd + Even 011โ‚‚ (3)| 010โ‚‚ (2)--------- 011โ‚‚ (3) โ† Still odd!โœ… Even + Even Only 010โ‚‚ (2)| 100โ‚‚ (4)--------- 110โ‚‚ (6) โ† Even result!โœ“
Understanding the Visualization
1
Observe Binary Patterns
Even numbers end in 0, odd numbers end in 1
2
Apply OR Logic
OR with any 1 bit produces 1 (odd result)
3
Find Valid Combinations
Only even-only combinations can produce trailing zeros
Key Takeaway
๐ŸŽฏ Key Insight: For bitwise OR to produce trailing zeros (even result), ALL selected numbers must be even. Count even numbers - if โ‰ฅ2 exist, answer is true!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array to count even numbers

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing a counter variable

n
2n
โœ“ Linear Space

Constraints

  • 2 โ‰ค nums.length โ‰ค 100
  • 1 โ‰ค nums[i] โ‰ค 100
  • All integers are positive
Asked in
Google 23 Amazon 18 Microsoft 15 Meta 12
24.7K Views
Medium Frequency
~8 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen