From 87b1174f4c465c8bea1cb93624d1fbccfbd733fe Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 7 Jan 2024 00:06:02 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=E2=80=9C1318.=20Minimum=20Flips=20?= =?UTF-8?q?to=20Make=20a=20OR=20b=20Equal=20to=20c=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/minimum-flips-to-make-a-or-b-equal-to-c.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 cs/minimum-flips-to-make-a-or-b-equal-to-c.cs diff --git a/cs/minimum-flips-to-make-a-or-b-equal-to-c.cs b/cs/minimum-flips-to-make-a-or-b-equal-to-c.cs new file mode 100644 index 0000000..532563b --- /dev/null +++ b/cs/minimum-flips-to-make-a-or-b-equal-to-c.cs @@ -0,0 +1,21 @@ +public class Solution { + public int MinFlips(int a, int b, int c) { + var flips = 0; + + for (; (a | b) != c; a >>= 1, b >>= 1, c >>= 1) { + var (aa, bb, cc) = (a & 1, b & 1, c & 1); + + if ((aa | bb) == cc) { + continue; + } + + if (aa != 0 && bb != 0) { + flips += 2; + } else { + ++flips; + } + } + + return flips; + } +}