Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[1,2,3,4,5]
[2,4,8,16]
[1,3,5,7,9]
count-odd-numbers-in-an-interval-range)remove-trailing-zeros-from-a-string)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2980: Check if Bitwise OR Has Trailing Zeros
class Solution {
public boolean hasTrailingZeros(int[] nums) {
int cnt = 0;
for (int x : nums) {
cnt += (x & 1 ^ 1);
}
return cnt >= 2;
}
}
// Accepted solution for LeetCode #2980: Check if Bitwise OR Has Trailing Zeros
func hasTrailingZeros(nums []int) bool {
cnt := 0
for _, x := range nums {
cnt += (x&1 ^ 1)
}
return cnt >= 2
}
# Accepted solution for LeetCode #2980: Check if Bitwise OR Has Trailing Zeros
class Solution:
def hasTrailingZeros(self, nums: List[int]) -> bool:
return sum(x & 1 ^ 1 for x in nums) >= 2
// Accepted solution for LeetCode #2980: Check if Bitwise OR Has Trailing Zeros
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2980: Check if Bitwise OR Has Trailing Zeros
// class Solution {
// public boolean hasTrailingZeros(int[] nums) {
// int cnt = 0;
// for (int x : nums) {
// cnt += (x & 1 ^ 1);
// }
// return cnt >= 2;
// }
// }
// Accepted solution for LeetCode #2980: Check if Bitwise OR Has Trailing Zeros
function hasTrailingZeros(nums: number[]): boolean {
let cnt = 0;
for (const x of nums) {
cnt += (x & 1) ^ 1;
}
return cnt >= 2;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.