Problem 540462 · medium · Phase 05 Advanced Algorithms & Graphs

Rewire Two Switch Panels

bit manipulation · case analysis

Two switch panels are described by the bit patterns a and b (bit i is switch i). A lamp board lights bit i when switch i is on in either panel, so it shows a | b. Flipping a single switch in either panel costs 1. Return the minimum number of flips so the board shows exactly the pattern c.

Examples

Input:  a = 12, b = 3, c = 10
Output: 2
Explanation: 1100 | 0011 must become 1010: turn off bit 0 in b and bit 2 in a.
Input:  a = 7, b = 7, c = 0
Output: 6

Constraints

  • 0 <= a, b, c < 2**60
  • Target complexity: O(number of bits).

Goals

  • Compare a, b and c one bit position at a time
  • Count the flips each case needs
  • Loop until all three numbers are exhausted
Starting Python…