Medium

Implement the missing code, denoted by ellipses. You may not modify the pre-existing code.

You're given two integers, n and m. Find position of the rightmost pair of equal bits in their binary representations (it is guaranteed that such a pair exists), counting from right to left.

Return the value of 2^position_of_the_found_pair (0-based).

Example

For n = 10 and m = 11, the output should be
equalPairOfBits(n, m) = 2.

10v10 = 1010v2, 11v10 = 1011v2, the position of the rightmost pair of equal bits is the bit at position 1 (0-based) from the right in the binary representations.
So the answer is 2^1 = 2.

Input/Output

  • [execution time limit] 0.5 seconds (cpp)

  • [input] integer n

    Guaranteed constraints:
    0 ≤ n ≤ 2^30.

  • [input] integer m

    Guaranteed constraints:
    0 ≤ m ≤ 2^30.

  • [output] integer

[C++] Syntax Tips

// Prints help message to the console
// Returns a string
std::string helloWorld(std::string name) {
    std::cout << "This prints to the console when you Run Tests" << std::endl;
    return "Hello, " + name;
}

더보기

Solution

int equalPairOfBits(int n, int m) {
  return n+m+1&~m-n;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Higher Version  (0) 2020.06.27
<Codesignal> Is Unstable Pair?  (0) 2020.06.26
<Codesignal> Different Rightmost Bit  (0) 2020.06.16
<Codesignal> Swap Adjacent Bits  (0) 2020.06.16
<Codesignal> Stolen Lunch  (0) 2020.05.29

+ Recent posts