Check if Bitwise OR Has Trailing Zeros - Problem

You are given an array of positive integers nums. You have to check if it is possible to select two or more elements in the array such that the bitwise OR of the selected elements has at least one trailing zero in its binary representation.

For example, the binary representation of 5, which is "101", does not have any trailing zeros, whereas the binary representation of 4, which is "100", has two trailing zeros.

Return true if it is possible to select two or more elements whose bitwise OR has trailing zeros, return false otherwise.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,1,4]
Output: true
💡 Note: We can select 2 and 4. Their OR is 2|4 = 6, which is 110 in binary (has one trailing zero).
Example 2 — All Odd Numbers
$ Input: nums = [1,3,5]
Output: false
💡 Note: All numbers are odd. Any OR combination will be odd (no trailing zeros). 1|3=3, 1|5=5, 3|5=7, 1|3|5=7.
Example 3 — Multiple Even Numbers
$ Input: nums = [2,4,6,8]
Output: true
💡 Note: Multiple even numbers exist. For example, 2|4 = 6 (binary: 110) has trailing zero.

Constraints

  • 2 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Bitwise OR Trailing Zeros INPUT nums = [2, 1, 4] 2 idx 0 1 idx 1 4 idx 2 Binary Form: 2 = 010 1 = 001 4 = 100 EVEN ODD EVEN Even nums end with 0 in binary ALGORITHM STEPS 1 Count Even Numbers Iterate through array 2 Check: num % 2 == 0 Identify even numbers 3 Increment Counter evenCount++ if even 4 Return Result evenCount >= 2 Even Count: 2 and 4 --> 2 2 >= 2 ? YES! FINAL RESULT Bitwise OR: 2 | 4 2 = 010 4 = 100 OR = 110 (6 in decimal) 110 ends with 0 --> Has trailing zero! Output: true Two even numbers found OR has trailing zero Key Insight: For OR result to have trailing zeros, ALL selected numbers must be even (end with 0 in binary). If even one odd number is included, the last bit becomes 1. Need at least 2 even numbers! TutorialsPoint - Check if Bitwise OR Has Trailing Zeros | Count Even Numbers Approach
Asked in
Google 15 Microsoft 12 Amazon 8
8.5K Views
Medium Frequency
~8 min Avg. Time
245 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