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.
Example 1:
Input: nums = [1,2,3,4,5] Output: true Explanation: If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.
Example 2:
Input: nums = [2,4,8,16] Output: true Explanation: If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero. Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are: (2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), and (2, 4, 8, 16).
Example 3:
Input: nums = [1,3,5,7,9] Output: false Explanation: There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR.
Constraints:
2 <= nums.length <= 1001 <= nums[i] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a collection of numbers and want to find out if you can pick at least two of them whose combination ends with a zero in binary form. The brute force strategy relies on testing every single pair of numbers possible to see if any pair meets this requirement.
Here's how the algorithm would work step-by-step:
def has_trailing_zeros(numbers_collection):
# We test every possible pair because the brute force strategy requires checking all combinations
for first_index in range(len(numbers_collection)):
for second_index in range(first_index + 1, len(numbers_collection)):
combined_value = numbers_collection[first_index] | numbers_collection[second_index]
# We check the least significant bit because a trailing zero in binary means the number is even
if (combined_value & 1) == 0:
return True
# If all pairs are exhausted without success, no valid pair exists in the collection
return False
Instead of testing every possible combination of numbers, the clever shortcut is to focus on a very specific feature of numbers that have trailing zeros in their binary form. Specifically, we only need to look at how many even numbers are present in the collection.
Here's how the algorithm would work step-by-step:
def has_trailing_zeros(integer_collection):
# We need to count even numbers because only they can contribute to trailing zeros.
even_number_count = 0
for current_number in integer_collection:
if current_number % 2 == 0:
even_number_count += 1
# At least two even numbers are required to guarantee a trailing zero in the bitwise OR result.
return even_number_count >= 2| Case | How to Handle |
|---|---|
| Array with exactly two elements | The algorithm checks this single pair directly and returns true if their bitwise OR has a trailing zero. |
| Numbers with no even integers in the array | Since trailing zeros in binary require an even number, the loop counts even numbers and returns false if the count is less than two. |
| Array containing all even numbers | Any two even numbers end in zero, so their bitwise OR will also end in zero and immediately return true. |
| Array with a mix of odd and even numbers where only one even number exists | Since selecting at least two elements is required and the OR of an odd and even number is odd, the solution correctly returns false. |
| Maximum-sized inputs with up to 100 elements | An O(N squared) nested loop approach runs well within time limits for such small constraints. |
| Large integer values up to the constraint limit | Standard 32-bit or 64-bit integers handle bitwise operations safely without any risk of overflow. |
| Array containing duplicate values | Duplicate even numbers naturally provide the necessary pair with trailing zeros, causing the algorithm to return true. |