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:
5in binary is"101"- no trailing zeros4in binary is"100"- two trailing zeros12in 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.
Constraints
- 2 โค nums.length โค 100
- 1 โค nums[i] โค 100
- All integers are positive
Visualization
Tap to expand
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code