Talentd
FleetCode

1318. Minimum Flips to Make a OR b Equal to c

Medium71.9% acceptance2 companies

Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.

Example 1:

Input: a = 2, b = 6, c = 5
Output: 3
Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)

Example 2:

Input: a = 4, b = 2, c = 7
Output: 1

Example 3:

Input: a = 1, b = 2, c = 3
Output: 0

Constraints:

  • 1 <= a <= 10^9
  • 1 <= b <= 10^9
  • 1 <= c <= 10^9

Asked at 2 companies

Hints open one at a time

Hint 1
Check the bits one by one whether they need to be flipped.
36:52
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minFlips(self, a: int, b: int, c: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
2
6
5
Input
4
2
7
Input
1
2
3
Your code is saved on this device.Solution