Leetcode 201 Bitwise AND of Numbers Range solution in c++ | Hindi Coding Community

0

 


Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.


Example 1:


Input: left = 5, right = 7

Output: 4

Example 2:


Input: left = 0, right = 0

Output: 0



class Solution {
public:
    int rangeBitwiseAnd(int left, int right) {
        long ans=left;
        for(long i=left; i<=right; i++) {
            ans &= i;
            if(ans==0) return 0;
        }
        return ans;
    }
};

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !