-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinimumUpdatesToMakeBitwiseOrEqualToTarget.java
67 lines (55 loc) · 1.81 KB
/
MinimumUpdatesToMakeBitwiseOrEqualToTarget.java
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
60
61
62
63
64
65
66
67
/*https://binarysearch.com/problems/Minimum-Updates-to-Make-Bitwise-OR-Equal-to-Target*/
import java.util.*;
class Solution {
public int solve(int a, int b, int target) {
char[] targetStr = Integer.toBinaryString(target).toCharArray();
char[] aStr = Integer.toBinaryString(a).toCharArray();
char[] bStr = Integer.toBinaryString(b).toCharArray();
int i, j, k, limit = Math.max(targetStr.length,Math.max(aStr.length,bStr.length)), count = 0;
targetStr = append(targetStr,limit);
aStr = append(aStr,limit);
bStr = append(bStr,limit);
i = j = k = 0;
while (i < limit)
{
if (targetStr[i] == '1' && aStr[j] == '0' && bStr[k] == '0') ++count;
else if (targetStr[i] == '0' && !(aStr[j] == '0' && bStr[k] == '0'))
{
if (aStr[j] == '1') ++count;
if (bStr[k] == '1') ++count;
}
++i; ++j; ++k;
}
return count;
}
public char[] append(char[] arr, int limit)
{
int i, j;
char[] temp = new char[limit];
for (i = 0; i < limit; ++i)
temp[i] = '0';
j = limit-1;
for (i = arr.length-1; i >= 0; --i)
temp[j--] = arr[i];
return temp;
}
}
import java.util.*;
class Solution {
public int solve(int a, int b, int target) {
int count = 0;
int aBit, bBit, targetBit;
while (a > 0 || b > 0 || target > 0)
{
aBit = a&1;
bBit = b&1;
targetBit = target&1;
if (targetBit == 1 && aBit == 0 && bBit == 0) ++count;
else if (targetBit == 0 && !(aBit == 0 && bBit == 0)) count += aBit+bBit;
a >>= 1;
b >>= 1;
target >>= 1;
}
return count;
}
}