Bitwise AND of Numbers range: Time Limit Exceeded but looks correct.
class Solution {
    public int rangeBitwiseAnd(int m, int n) {
        int currentPosition = m + 1;
        int result = 0;
        if(m == n) {
            result = m & n;
            return result;
        }
        while(currentPosition <= n){
            if(result == 0){
                result = m & currentPosition;
                if(result == 0) {
                    break;
                }
                currentPosition++;
            }
            else {
                result &= currentPosition;
                if(result == 0){
                    break;
                }
                currentPosition++;
            }
        }
        return result;
    }
}

I am getting 'Time Limit exceeded' but I feel that the logic is correct for all the test cases to pass. Kindly have a look at it. Thanks.

Comments (0)