1318. Minimum Flips to Make a OR b Equal to c LeetCode Solution
In this guide, you will get 1318. Minimum Flips to Make a OR b Equal to c LeetCode Solution with the best time and space complexity. The solution to Minimum Flips to Make a OR b Equal to c problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Flips to Make a OR b Equal to c solution in C++
- Minimum Flips to Make a OR b Equal to c solution in Java
- Minimum Flips to Make a OR b Equal to c solution in Python
- Additional Resources

Problem Statement of Minimum Flips to Make a OR b Equal to c
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
Complexity Analysis
- Time Complexity: O(30) = O(1)
- Space Complexity: O(1)
1318. Minimum Flips to Make a OR b Equal to c LeetCode Solution in C++
class Solution {
public:
int minFlips(int a, int b, int c) {
constexpr int kMaxBit = 30;
int ans = 0;
for (int i = 0; i < kMaxBit; ++i)
if (c >> i & 1)
ans += (a >> i & 1) == 0 && (b >> i & 1) == 0;
else // (c >> i & 1) == 0
ans += (a >> i & 1) + (b >> i & 1);
return ans;
}
};
/* code provided by PROGIEZ */
1318. Minimum Flips to Make a OR b Equal to c LeetCode Solution in Java
class Solution {
public int minFlips(int a, int b, int c) {
final int kMaxBit = 30;
int ans = 0;
for (int i = 0; i < kMaxBit; ++i)
if ((c >> i & 1) == 1)
ans += ((a >> i & 1) == 0 && (b >> i & 1) == 0) ? 1 : 0;
else // (c >> i & 1) == 0
ans += ((a >> i & 1) == 1 ? 1 : 0) + ((b >> i & 1) == 1 ? 1 : 0);
return ans;
}
}
// code provided by PROGIEZ
1318. Minimum Flips to Make a OR b Equal to c LeetCode Solution in Python
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
kMaxBit = 30
ans = 0
for i in range(kMaxBit):
if c >> i & 1:
ans += (a >> i & 1) == 0 and (b >> i & 1) == 0
else: # (c >> i & 1) == 0
ans += (a >> i & 1) + (b >> i & 1)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.