Skip to content

Commit cd40c23

Browse files
committed
flipping birds
1 parent b20b5ec commit cd40c23

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

flipping-bits.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
process.stdin.resume();
6+
process.stdin.setEncoding('utf-8');
7+
8+
let inputString = '';
9+
let currentLine = 0;
10+
11+
process.stdin.on('data', inputStdin => {
12+
inputString += inputStdin;
13+
});
14+
15+
process.stdin.on('end', function () {
16+
inputString = inputString.replace(/\s*$/, '')
17+
.split('\n')
18+
.map(str => str.replace(/\s*$/, ''));
19+
20+
main();
21+
});
22+
23+
function readLine() {
24+
return inputString[currentLine++];
25+
}
26+
27+
const getBinaryNumber = (N) => {
28+
let number = N.toString(2);
29+
let str = '';
30+
let n = 32 - number.length;
31+
while (n--){
32+
str+='0';
33+
}
34+
return str+number;
35+
}
36+
37+
const flipbits = (bits) => {
38+
let str = '';
39+
let n = 0;
40+
while (n < bits.length){
41+
str+= (bits[n] === '0') ? '1': '0';
42+
n++;
43+
}
44+
return str;
45+
}
46+
47+
function flippingBits(N) {
48+
let bits = getBinaryNumber(N);
49+
return parseInt(flipbits(bits), 2);
50+
}
51+
52+
function main() {
53+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
54+
55+
const q = parseInt(readLine(), 10);
56+
57+
for (let qItr = 0; qItr < q; qItr++) {
58+
const n = parseInt(readLine(), 10);
59+
60+
const result = flippingBits(n);
61+
62+
ws.write(result + '\n');
63+
}
64+
65+
ws.end();
66+
}

maximizing-xor.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
process.stdin.resume();
6+
process.stdin.setEncoding('utf-8');
7+
8+
let inputString = '';
9+
let currentLine = 0;
10+
11+
process.stdin.on('data', inputStdin => {
12+
inputString += inputStdin;
13+
});
14+
15+
process.stdin.on('end', _ => {
16+
inputString = inputString.replace(/\s*$/, '')
17+
.split('\n')
18+
.map(str => str.replace(/\s*$/, ''));
19+
20+
main();
21+
});
22+
23+
function readLine() {
24+
return inputString[currentLine++];
25+
}
26+
27+
// Complete the maximizingXor function below.
28+
function maximizingXor(l, r) {
29+
let rightIncrement = l;
30+
let start = l;
31+
let max = Number.MIN_VALUE;
32+
while (l <= r) {
33+
// console.log(l,rightIncrement)
34+
if ((l ^ rightIncrement) > max) {
35+
max = l ^ rightIncrement;
36+
}
37+
if (rightIncrement === r) {
38+
l++;
39+
rightIncrement = start;
40+
}
41+
rightIncrement++;
42+
}
43+
return max;
44+
}
45+
46+
function main() {
47+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
48+
49+
const l = parseInt(readLine(), 10);
50+
51+
const r = parseInt(readLine(), 10);
52+
53+
let result = maximizingXor(l, r);
54+
55+
ws.write(result + "\n");
56+
57+
ws.end();
58+
}

sum-vs-xor.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
process.stdin.resume();
6+
process.stdin.setEncoding('utf-8');
7+
8+
let inputString = '';
9+
let currentLine = 0;
10+
11+
process.stdin.on('data', function (inputStdin) {
12+
inputString += inputStdin;
13+
});
14+
15+
process.stdin.on('end', function () {
16+
inputString = inputString.split('\n');
17+
18+
main();
19+
});
20+
21+
function readLine() {
22+
return inputString[currentLine++];
23+
}
24+
25+
// Complete the sumXor function below.
26+
function sumXor(n) {
27+
let c = 0;
28+
while (n) {
29+
c += n % 2 ? 0 : 1;
30+
n = Math.floor(n/2);
31+
}
32+
c = Math.pow(2, c);
33+
return c;
34+
}
35+
36+
function main() {
37+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
38+
39+
const n = parseInt(readLine().trim(), 10);
40+
41+
const result = sumXor(n);
42+
43+
ws.write(result + '\n');
44+
45+
ws.end();
46+
}

0 commit comments

Comments
 (0)