Skip to content

Commit cce9dab

Browse files
committed
count pairs
1 parent bab0dda commit cce9dab

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Data-Structure/Hashing/countPairs.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
function countPairsDivideOneElement(array) {
3+
let hm = new Map();
4+
let maxValue = -Infinity;
5+
let countPairs = 0;
6+
for (let index = 0; index < array.length; index++) {
7+
if (maxValue < array[index]) maxValue = array[index];
8+
hm.set(array[index]);
9+
}
10+
for (let index = 0; index < array.length; index++) {
11+
let multiply = 2;
12+
let k = array[index] * multiply;
13+
while (k <= maxValue) {
14+
if (hm.has(k)) countPairs++;
15+
k = array[index] * (++multiply);
16+
}
17+
}
18+
return countPairs;
19+
}
20+
let array = [1, 2, 3];
21+
console.log('Number of pairs that one element of pair divides other = ', countPairsDivideOneElement(array));

0 commit comments

Comments
 (0)