forked from NKaty/Algorithms-and-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber-complement.js
More file actions
57 lines (48 loc) · 2.16 KB
/
number-complement.js
File metadata and controls
57 lines (48 loc) · 2.16 KB
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
// Given a positive integer num, output its complement number.
// The complement strategy is to flip the bits of its binary representation.
// Constraints:
// The given integer num is guaranteed to fit within the range of a 32-bit signed integer.
// num >= 1
// You could assume no leading zero bit in the integer’s binary representation.
const findComplement = (num) => {
// For example 100 --> 27:
// 100 = 00000000000000000000000001100100
// mostSignificantBit = 00000000000000000000000001000000
// mostSignificantBit - 1 = 00000000000000000000000000111111
// ~num 11111111111111111111111110011011
// ~num & (mostSignificantBit - 1) = 00000000000000000000000000011011
const mostSignificantBit = 2 ** Math.floor(Math.log2(num));
return ~num & (mostSignificantBit - 1);
};
console.log(findComplement(100)); // 27
console.log(findComplement(967)); // 56
console.log(findComplement(1)); // 0
const findComplementXOR = (num) => {
// For example 100 --> 27:
// 100 = 00000000000000000000000001100100
// mostSignificantBit = 00000000000000000000000001000000
// mostSignificantBit << 1 = 00000000000000000000000010000000
// (mostSignificantBit << 1) - 1 = 00000000000000000000000001111111
// num ^ (mostSignificantBit << 1) - 1 = 00000000000000000000000000011011
const mostSignificantBit = 2 ** Math.floor(Math.log2(num));
return num ^ ((mostSignificantBit << 1) - 1);
};
console.log(findComplementXOR(100)); // 27
console.log(findComplementXOR(967)); // 56
console.log(findComplementXOR(1)); // 0
const findComplementIterative = (num) => {
// After while loop: i == ((mostSignificantBit << 1) - 1) from findComplementXOR
// For example 100 --> 27:
// 100 = 00000000000000000000000001100100
// i = 00000000000000000000000001111111
// num ^ i = 00000000000000000000000000011011
let i = 1;
while (i < num) {
i <<= 1;
i++;
}
return num ^ i;
};
console.log(findComplementIterative(100)); // 27
console.log(findComplementIterative(967)); // 56
console.log(findComplementIterative(1)); // 0