![]() |
VOOZH | about |
Given a non-negative number n and two values l and r. The problem is to count the number of unset bits in the range l to r in the binary representation of n, i.e, to count unset bits from the rightmost lth bit to the rightmost rth bit.
Examples:
Input : n = 42, l = 2, r = 5 Output : 2 (42)10 = (101010)2 There are '2' unset bits in the range 2 to 5. Input : n = 80, l = 1, r = 4 Output : 4
Approach: Following are the steps:
Output:
4
Time Complexity: O(log n)
Space Complexity: O(1)