Simple and Clear Code | Logarithmic Time Complexity | With Best and Easy-to-Understand Explanation.
Intuition and LogicCode BlockComplexity

Time complexity: O(log(n)), n=max(a,b,c)


Space complexity: O(1)

CodeJava more Clean Code
# Intuition and Logic
<pre><h4>bit value of a, b and c can be either 0 or 1. 
Bitwise OR results, 
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
And to make (a bitwise OR b) = c (a | b = c), 
there can be only following cases</h4>
<h5>if c = 0, then
    case 1: a = 0 and b = 0 => minimum Flip = 0
    case 2: a = 0 and b = 1 => minimum Flip = 1 (flip b to 0)
    case 3: a = 1 and b = 0 => minimum Flip = 1 (flip a to 0)
    case 4: a = 1 and b = 1 => minimum Flip = 2 (flip a to 0 & b to 0)
if, c = 1, then
    case 1: a = 0 and b = 0 => minimum Flip = 1 (flip a to 1 or b to 1)
    case 2: a = 0 and b = 1 => minimum Flip = 0
    case 3: a = 1 and b = 0 => minimum Flip = 0
    case 4: a = 1 and b = 1 => minimum Flip = 0
</h5></pre>


# Complexity
- ***Time complexity***: $$O(log(n))$$, $$n = max(a, b, c)$$

- ***Space complexity***: $$O(1)$$

# Code
```java []
class Solution {
    public int minFlips(int a, int b, int c) {
        int n = Math.max(a, Math.max(b, c));
        int minFlips = 0;

        int i = 1;
        int x = 0, y = 0, z = 0;
        while(i <= n) {
            x = a & i;
            y = b & i;
            z = c & i;

            if(z == 0) {
                if(x >= 1) {
                    ++minFlips;
                }
                if(y >= 1) {
                    ++minFlips;
                }
            } else {
                if(x == 0 && y == 0) {
                    ++minFlips;
                }
            }

            i <<= 1;
        }

        return minFlips;
    }
}
```
```C []
int minFlips(int a, int b, int c){
    int n = a > b ? a > c ? a : c : b > c ? b : c;
    int minFlips = 0;

    int i = 1;
    int x = 0, y = 0, z = 0;
    while(i <= n) {
        x = a & i;
        y = b & i;
        z = c & i;

        if(z == 0) {
            if(x >= 1) {
                ++minFlips;
            }
            if(y >= 1) {
                ++minFlips;
            }
        } else {
            if(x == 0 && y == 0) {
                ++minFlips;
            }
        }

        i <<= 1;
    }

    return minFlips;
}
```
```C++ []
class Solution {
public:
    int minFlips(int a, int b, int c) {
        int n = max(a, max(b, c));
        int minFlips = 0;

        int i = 1;
        int x = 0, y = 0, z = 0;
        while(i <= n) {
            x = a & i;
            y = b & i;
            z = c & i;

            if(z == 0) {
                if(x >= 1) {
                    ++minFlips;
                }
                if(y >= 1) {
                    ++minFlips;
                }
            } else {
                if(x == 0 && y == 0) {
                    ++minFlips;
                }
            }

            i <<= 1;
        }

        return minFlips;
    }
};
``` 
```C# []
public class Solution {
    public int MinFlips(int a, int b, int c) {
        int n = Math.Max(a, Math.Max(b, c));
        int minFlips = 0;

        int i = 1;
        int x = 0, y = 0, z = 0;
        while(i <= n) {
            x = a & i;
            y = b & i;
            z = c & i;

            if(z == 0) {
                if(x >= 1) {
                    ++minFlips;
                }
                if(y >= 1) {
                    ++minFlips;
                }
            } else {
                if(x == 0 && y == 0) {
                    ++minFlips;
                }
            }

            i <<= 1;
        }

        return minFlips;
    }
}
```
```Python []
class Solution(object):
    def minFlips(self, a, b, c):
        n = max(a, b, c)
        minFlips = 0

        i = 1
        while(i <= n):
            x = a & i
            y = b & i
            z = c & i

            if(z == 0):
                if(x >= 1):
                    minFlips += 1
                if(y >= 1):
                    minFlips += 1
            else:
                if(x == 0 and y == 0):
                    minFlips += 1

            i <<= 1

        return minFlips  
```
```JavaScript []
/**
 * @param {number} a
 * @param {number} b
 * @param {number} c
 * @return {number}
 */
var minFlips = function(a, b, c) {
    let n = Math.max(a, b, c);
    let minFlips = 0;

    let i = 1;
    let x = 0, y = 0, z = 0;
    while(i <= n) {
        x = a & i;
        y = b & i;
        z = c & i;

        if(z == 0) {
            if(x >= 1) {
                ++minFlips;
            }
            if(y >= 1) {
                ++minFlips;
            }
        } else {
            if(x == 0 && y == 0) {
                ++minFlips;
            }
        }

        i <<= 1;
    }

    return minFlips;
};
```
``` Kotlin []
class Solution {
    fun minFlips(a: Int, b: Int, c: Int): Int {
        val n = maxOf(a, b, c)
        var minFlips = 0

        var i = 1
        while(i <= n) {
            var x = a and i
            var y = b and i
            var z = c and i

            if(z == 0) {
                if(x >= 1) {
                    ++minFlips
                }
                if(y >= 1) {
                    ++minFlips
                }
            } else {
                if(x == 0 && y == 0) {
                    ++minFlips
                }
            }

            i = i shl 1
        }

        return minFlips
    }
}
```
``` TypeScript []
function minFlips(a: number, b: number, c: number): number {
    let n = Math.max(a, b, c);
    let minFlips = 0;

    let i = 1;
    let x = 0, y = 0, z = 0;
    while(i <= n) {
        x = a & i;
        y = b & i;
        z = c & i;

        if(z == 0) {
            if(x >= 1) {
                ++minFlips;
            }
            if(y >= 1) {
                ++minFlips;
            }
        } else {
            if(x == 0 && y == 0) {
                ++minFlips;
            }
        }

        i <<= 1;
    }

    return minFlips;
};
```
```Python3 []
class Solution:
    def minFlips(self, a: int, b: int, c: int) -> int:
        n = max(a, b, c)
        minFlips = 0

        i = 1
        while(i <= n):
            x = a & i
            y = b & i
            z = c & i

            if(z == 0):
                if(x >= 1):
                    minFlips += 1
                if(y >= 1):
                    minFlips += 1
            else:
                if(x == 0 and y == 0):
                    minFlips += 1

            i <<= 1

        return minFlips
```

# Java more Clean Code
```java []
class Solution {
    public int minFlips(int a, int b, int c) {
        int n = Math.max(a, Math.max(b, c));
        
        int minFlips = 0;
        for(int i=1; i<=n; i<<=1) {
            int x = a & i, y = b & i, z = c & i;
            if(z == 0) {
                minFlips += (x > 0 ? 1 : 0) + (y > 0 ? 1 : 0);
            } else {
                minFlips += (x == 0 && y == 0) ? 1 : 0;
            }
        }

        return minFlips;
    }
}
```
['Bit Manipulation', 'C', 'Python', 'C++', 'Java', 'TypeScript', 'Python3', 'Kotlin', 'JavaScript', 'C#']