알고리즘/codefights

19>rangeBitCount

Diademata 2021. 4. 17. 17:09
반응형

For a = 2 and b = 7, the output should be
rangeBitCount(a, b) = 11.

Given a = 2 and b = 7 the array is: [2, 3, 4, 5, 6, 7]. Converting the numbers to binary, we get [10, 11, 100, 101, 110, 111], which contains 1 + 2 + 1 + 2 + 2 + 3 = 11 1s.

Input/Output

[execution time limit] 0.5 seconds (cpp)

[input] integer a

Guaranteed constraints:
0 ≤ a ≤ b.

[input] integer b

Guaranteed constraints:
a ≤ b ≤ 10.

 

code>>

 

int rangeBitCount(int aint b) {

    auto count = 0;

    for (int i = ai <= b; ++i)

    {

        std::bitset<4bit(i);

        count += bit.count();

    }

    return count;

}

 

반응형