Menu Close

Leetcode 2980: Check if Bitwise OR Has Trailing Zeros Solution

Here, we are going to see Check if Bitwise OR Has Trailing Zeros Solution of leetcode 2980 problem with code and example in C++.

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).

Check if Bitwise OR Has Trailing Zeros Solution of leetcode 2980 in C++:

Here, we will be solving problem in multiple ways with code.

C++ code 1:

class Solution {

public:

    bool hasTrailingZeros(vector<int>& nums) {

        int count = 0;
        

        for(int i = 0; i < nums.size(); i++) {

            if((nums[i] & 1) == 0) {

                count++;

            }

            if(count > 1) {

                return true;

            }

        }

        return false;

    }

};

C++ code 2:

class Solution {

public:

    bool hasTrailingZeros(vector<int>& nums) {

        int cnt = 0;

        for (auto n : nums) {

            if (n % 2 == 0) cnt++;

            if (cnt == 2) return true;

        }

        return false;

    }
};

Output:

Input: nums = [1,2,3,4,5]
Output: true

To check more leetcode problem’s solution. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

https://www.techieindoor.com/category/interview-questions/

https://leetcode.com/contest/weekly-contest-378/problems/check-if-bitwise-or-has-trailing-zeros/

Posted in C++, Easy, Leetcode

Leave a Reply

Your email address will not be published. Required fields are marked *