Check if Bitwise OR Has Trailing Zeros

Problem Description

So, you think you can just throw a bunch of numbers together and expect them to behave nicely? Well, think again! The problem at hand is to check if the Bitwise OR of a list of integers has trailing zeros. Yes, trailing zeros! Just like that one friend who always shows up late to the party, trailing zeros can be quite the nuisance.

Imagine you’re at a buffet, and you see a plate of food with a few crumbs left. You might think, “Hey, that’s not enough for me!” Similarly, in our problem, we want to ensure that the Bitwise OR of the numbers has at least two trailing zeros. If it does, we can confidently say, “Yes, this is a feast!” If not, well, it’s just crumbs.

Code Solution

class Solution:
    def hasTrailingZeros(self, nums: list[int]) -> bool:
        return sum(num % 2 == 0 for num in nums) >= 2

Approach

The approach here is as straightforward as it gets. We simply iterate through the list of numbers and check how many of them are even (i.e., divisible by 2). If we find at least two even numbers, we can confidently declare that the Bitwise OR has trailing zeros. It’s like counting how many friends brought snacks to the party—if two or more did, you’re in for a good time!

Time and Space Complexity

Time Complexity: O(n), where n is the number of elements in the input list. We need to check each number once.

Space Complexity: O(1), as we are using a constant amount of space regardless of the input size.

Real-World Example

Let’s say you’re organizing a game night with your friends. You want to ensure that at least two of them bring snacks. If they do, you can have a great time munching away while playing games. If only one or none bring snacks, well, it’s just going to be a sad evening with empty hands. Similarly, in our problem, we need at least two even numbers to ensure that the Bitwise OR has trailing zeros.