Beats 100% | Easy 0 ms Solution w/ Detailed Explanation.
\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nI simulated the whole process by running through Example 1 using pen and paper 
![image.png](https://assets.leetcode.com/users/images/318ea6c0-0f60-4735-951c-225fbfc98a41_1726059897.905723.png)\n<hr>\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI simulated the whole process by running through `Example 1` using pen and paper (you should too!, take out your notebook and a pen or pencil and try to simulate it before reading this any further).\n\nThe main thing here is that we need to examine the bits of `a`, `b`, and `c` one by one (from the least significant bit to the most significant bit). We compare these bits and decide whether flipping is necessary based on what the corresponding bit in `c` is supposed to be.\n\n### Main Insights :\n- If the current bit in `c` is `1`, then at least one of the bits in `a` or `b` must be `1` or else `a OR b` will not be equal to `c`. If both are `0`, then we need one flip (either bit `a` or bit `b`) to make `a OR b` equal to `c`.\n- If the current bit in `c` is `0`, then both bits in `a` and `b` must be `0`. If one or both are `1`, we need to flip them back to `0`.\n\nThat\u2019s the whole trick to solving this question! We keep repeating this process for each bit of `a`, `b`, and `c` until all the bits have been handled.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**1. Compare Bits :** We compare the least significant bits (LSBs, the rightmost bit in a binary number) of `a`, `b`, and `c`. For each bit, we decide whether it satisfies the conditions based on the bit value of `c`.\n\n**2. Bitwise OR Logic :**\n- If the current bit in `c` is `1`, then at least one of the bits from `a` or `b` should be `1`. If both are `0`, then we have to flip either bit `a` or bit `b`. Therefore, we increase the count by 1.\n- If the current bit in `c` is `0`, both bits in `a` and `b` must be `0`. Any `1` bit in `a` or `b` should be flipped to `0`, and we add those flips to our count.\n\n**3. Shifting Bits :** After processing the current bits, we shift `a`, `b`, and `c` one position to the right (basically dividing by `2`) to move on to the next bit.\n\n**4. Repeat Until Done :** We repeat the process until all the bits in `a`, `b`, and `c` are processed.\n\n### Let\'s Understand Better w/ an Example :\n\nLet\u2019s say `a = 2`, `b = 6`, and `c = 5`.\n\n`a = 2` in binary is `010`\n`b = 6` in binary is `110`\n`c = 5` in binary is `101`\n\nFor each bit, we compare the LSBs :\n\n - **1st bit (LSB) :** `0` (a), `0` (b), `1` (c) \u2192 1 bit flip (either that of `a` or `b`) is needed to fullfil the condition : `a OR b == c`.\n- **2nd bit :** `1` (a), `1` (b), `0` (c) \u2192 Flip both bits in a and b (2 flips).\n- **3rd bit :** `0` (a), `1` (b), `1` (c) \u2192 No flips needed as the condition `a OR b == c` is already satisfied.\n\nTherefore, the total bit flips = 3.\n\n# Complexity\n\n- **Time complexity :** $$O(log N)$$, where `N` is the maximum number of bits in `a`, `b`, or `c`. Each iteration processes one bit, and the number of iterations depends on the number of bits.\n- **Space complexity :** $$O(1)$$ since no additional space is required beyond the input variables and a few fixed size variables.\n\n# Code\n```java []\nclass Solution {\n    public int minFlips(int a, int b, int c) {\n        int count = 0;\n        while (a != 0 || b != 0 || c != 0) {\n            int lsbA = a & 1; // Least significant bit of a.\n            int lsbB = b & 1; // Least significant bit of b.\n            int lsbC = c & 1; // Least significant bit of c.\n\n            // Case when the current bit in c is 1.\n            if (lsbC == 1) {\n                if (lsbA == 0 && lsbB == 0) {\n                    count++; // We need one flip if both a and b bits are 0.\n                }\n            }\n            // Case when the current bit in c is 0.\n            else {\n                count += lsbA + lsbB; // Add flips for non-zero bits in a and b.\n            }\n            // Shift a, b, and c to the right to check the next bit.\n            a >>= 1;\n            b >>= 1;\n            c >>= 1;\n        }\n\n        return count;\n    }\n}\n\n```\n<hr>\n\nIf you found this solution helpful, please consider upvoting! \uD83D\uDC4D. Your support is greatly appreciated \u2764\uFE0F.\n
['Bit Manipulation', 'Java']