Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1318: Minimum Flips to Make a OR b Equal to c

In this guide, we solve Leetcode #1318 Minimum Flips to Make a OR b Equal to c in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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 ).

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Bit Manipulation

Intuition

The problem structure lets us track state with bitwise operations.

Bit operations are constant time and avoid extra memory.

Approach

Apply XOR/AND/OR and shifts to maintain the required invariant.

Aggregate the result in a single pass.

Steps:

  • Identify a bitwise invariant.
  • Combine values with bit operations.
  • Return the aggregated result.

Example

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

Python Solution

class Solution: def minFlips(self, a: int, b: int, c: int) -> int: ans = 0 for i in range(32): x, y, z = a >> i & 1, b >> i & 1, c >> i & 1 ans += x + y if z == 0 else int(x == 0 and y == 0) return ans

Complexity

The time complexity is O(log⁡M)O(\log M)O(logM), where MMM is the maximum value of the numbers in the problem. The space complexity is O(1)O(1)O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy