← Home

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

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
bit manipulationC++Markdown
131

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1318. Minimum Flips to Make a OR b Equal to c, the solution in this repository is mainly a bit manipulation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: bit manipulation.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are minFlips.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Minimum Flips to Make a OR b Equal to c.
02//Memory Usage: 7.2 MB, less than 100.00% of C++ online submissions for Minimum Flips to Make a OR b Equal to c.
03
04class Solution {
05public:
06    int minFlips(int a, int b, int c) {
07        int flips = 0;
08        
09        for(int i = 0; (1 << i) <= max(max(a, b), c); i++){
10            //check bit by bit
11            int bit = 1 << i;
12            int abit = a & bit ? 1 : 0;
13            int bbit = b & bit ? 1 : 0;
14            int cbit = c & bit ? 1 : 0;
15            // cout << abit << " " << bbit << " " << cbit << endl;
16            if(!(abit | bbit) && cbit){
17                //both abit and bbit are 0
18                //change a and b's current bit from 1 to 0
19                flips++;
20            }else if((abit & bbit) && !cbit){
21                //both abit and bbit are 1
22                //change a and b's current bit from 1 to 0
23                flips+=2;
24            }else if((abit ^ bbit) && !cbit){
25                //one of abit and bbit is 0 and another is 1
26                flips++;
27            }
28        }
29        
30        return flips;
31    }
32};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.