#2980

Check if Bitwise OR Has Trailing Zeros

Easy
ArrayBit Manipulation
LeetCode ↗

Approaches

💡

Intuition

Time Space

The brute force approach involves checking every possible pair of elements in the array to see if their bitwise OR has trailing zeros. This is straightforward but inefficient for larger arrays.

⚙️

Algorithm

5 steps
  1. 1Step 1: Initialize a loop to iterate through each element in the array.
  2. 2Step 2: For each element, initialize another loop to check every subsequent element.
  3. 3Step 3: Calculate the bitwise OR of the two selected elements.
  4. 4Step 4: Check if the result has trailing zeros by checking if the result & 1 equals 0.
  5. 5Step 5: If a valid pair is found, return true. If no pairs are found after all iterations, return false.
solution.py12 lines
1# Full working Python code
2
3def has_trailing_zero(nums):
4    n = len(nums)
5    for i in range(n):
6        for j in range(i + 1, n):
7            if (nums[i] | nums[j]) & 1 == 0:
8                return True
9    return False
10
11# Example usage
12print(has_trailing_zero([1, 2, 3, 4, 5]))  # Output: True

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.