Skip to content

Commit c17e9d7

Browse files
committed
#few medium problems
1 parent e4df27e commit c17e9d7

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

count-triplets.js

+66
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', 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+
function put(dict, key) {
25+
dict[key] = dict[key] ? (dict[key] + 1) : 1;
26+
}
27+
// Complete the countTriplets function below.
28+
function countTriplets(arr, r) {
29+
let a = {};
30+
arr.forEach(x => {
31+
put(a, x);
32+
});
33+
let b = {};
34+
let s = 0;
35+
console.log('a', a);
36+
arr.forEach(i => {
37+
let j = i / r;
38+
let k = i * r;
39+
40+
a[i] = a[i] - 1;
41+
if (b[j] && a[k]) {
42+
console.log(j, i, k);
43+
s += b[j] * a[k];
44+
}
45+
put(b, i);
46+
});
47+
return s;
48+
}
49+
50+
function main() {
51+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
52+
53+
const nr = readLine().replace(/\s+$/g, '').split(' ');
54+
55+
const n = parseInt(nr[0], 10);
56+
57+
const r = parseInt(nr[1], 10);
58+
59+
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
60+
61+
const ans = countTriplets(arr, r);
62+
63+
ws.write(ans + '\n');
64+
65+
ws.end();
66+
}

0 commit comments

Comments
 (0)