-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1318. Minimum Flips to Make a OR b Equal to c.cpp
59 lines (56 loc) · 1.27 KB
/
1318. Minimum Flips to Make a OR b Equal to c.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution {
public:
int minFlips(int a, int b, int c) {
bitset<32>a1(a);
bitset<32>b1(b);
bitset<32>c1(c);
int n=a1.size(),m=b1.size();
int cnt=0;
int i=0,j=0;
int k=0;
while(i<n && j<m)
{
int x=a1[i]|b1[i];
if(x!=c1[k])
{
if(c1[k]==0)
{
if(a1[i]==1 && b1[i]==0)
cnt++;
else if(a1[i]==0 && b1[i]==1)
cnt++;
else
cnt+=2;
}
else
{
cnt++;
}
}
i++;
k++;
j++;
}
if(i==n && j<m)
{
while(j<m)
{
if(b1[j]!=c1[k])
cnt++;
j++;
k++;
}
}
if(j==m && i<n)
{
while(i<n)
{
if(a1[i]!=c1[k])
cnt++;
i++;
k++;
}
}
return cnt;
}
};