Skip to content

Commit 671da96

Browse files
authored
Merge pull request #470 from sir-gon/feature/count_triplets_1
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: C…
2 parents 8b90f90 + a525def commit 671da96

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md]]
3+
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md]]
4+
*/
5+
import { logger as console } from '../../../logger.js';
6+
7+
export function countTriplets(arr, ratio) {
8+
const size = arr.length;
9+
let counter = 0;
10+
11+
for (let i = 0; i < size - 2; i++) {
12+
for (let j = i + 1; j < size - 1; j++) {
13+
for (let k = j + 1; k < size; k++) {
14+
console.debug(`${arr[i]}, ${arr[j]}, ${arr[k]}`);
15+
16+
if (ratio * arr[i] === arr[j] && ratio * arr[j] === arr[k]) {
17+
counter += 1;
18+
}
19+
}
20+
}
21+
}
22+
23+
return counter;
24+
}
25+
26+
export default { countTriplets };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import { countTriplets } from './count_triplets_1_bruteforce.js';
5+
6+
const SMALL_TEST_CASES = [
7+
{
8+
title: 'Sample Test Case 0',
9+
input: [1, 2, 2, 4],
10+
r: 2,
11+
expected: 2
12+
},
13+
{
14+
title: 'Sample Test Case 1',
15+
input: [1, 3, 9, 9, 27, 81],
16+
r: 3,
17+
expected: 6
18+
},
19+
{
20+
title: 'Sample Test Case 1 (unsorted)',
21+
input: [9, 3, 1, 81, 9, 27],
22+
r: 3,
23+
expected: 1
24+
},
25+
{
26+
title: 'Sample Test Case 12',
27+
input: [1, 5, 5, 25, 125],
28+
r: 5,
29+
expected: 4
30+
}
31+
];
32+
33+
describe('count_triplets_1', () => {
34+
it('countTriplets test cases', () => {
35+
expect.assertions(4);
36+
37+
SMALL_TEST_CASES.forEach((test) => {
38+
const answer = countTriplets(test.input, test.r);
39+
40+
console.debug(
41+
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
42+
);
43+
44+
expect(answer).toStrictEqual(test.expected);
45+
});
46+
});
47+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import { countTriplets } from './count_triplets_1_optmized.js';
5+
import SMALL_TEST_CASES from './count_triplets_1_testcases.json';
6+
7+
const BIG_TEST_CASES = [
8+
{
9+
title: 'Sample Test Case 2',
10+
input: [
11+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
14+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
15+
],
16+
r: 1,
17+
expected: 161700
18+
}
19+
];
20+
21+
describe('count_triplets_1 (optimized)', () => {
22+
it('countTriplets small test cases', () => {
23+
expect.assertions(4);
24+
25+
SMALL_TEST_CASES.forEach((test) => {
26+
const answer = countTriplets(test.input, test.r);
27+
28+
console.debug(
29+
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
30+
);
31+
32+
expect(answer).toStrictEqual(test.expected);
33+
});
34+
});
35+
36+
it('countTriplets big test cases', () => {
37+
expect.assertions(1);
38+
39+
BIG_TEST_CASES.forEach((test) => {
40+
const answer = countTriplets(test.input, test.r);
41+
42+
console.debug(
43+
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
44+
);
45+
46+
expect(answer).toStrictEqual(test.expected);
47+
});
48+
});
49+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md]]
3+
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md]]
4+
*/
5+
6+
export function countTriplets(arr, ratio) {
7+
let triplets = 0;
8+
9+
const aCounter = arr.reduce((accumulator, entry) => {
10+
if (entry in accumulator) {
11+
accumulator[entry] += 1;
12+
} else {
13+
accumulator[entry] = 1;
14+
}
15+
return accumulator;
16+
}, {});
17+
18+
const bCounter = {};
19+
20+
arr.forEach((x) => {
21+
const j = Math.floor(x / ratio);
22+
const k = x * ratio;
23+
aCounter[x] -= 1;
24+
if (bCounter[j] && aCounter[k] && x % ratio === 0) {
25+
triplets += bCounter[j] * aCounter[k];
26+
}
27+
28+
if (x in bCounter) {
29+
bCounter[x] += 1;
30+
} else {
31+
bCounter[x] = 1;
32+
}
33+
});
34+
35+
return triplets;
36+
}
37+
38+
export default { countTriplets };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"input": [1, 2, 2, 4],
5+
"r": 2,
6+
"expected": 2
7+
},
8+
{
9+
"title": "Sample Test Case 1",
10+
"input": [1, 3, 9, 9, 27, 81],
11+
"r": 3,
12+
"expected": 6
13+
},
14+
{
15+
"title": "Sample Test Case 1 (unsorted)",
16+
"input": [9, 3, 1, 81, 9, 27],
17+
"r": 3,
18+
"expected": 1
19+
},
20+
{
21+
"title": "Sample Test Case 12",
22+
"input": [1, 5, 5, 25, 125],
23+
"r": 5,
24+
"expected": 4
25+
}
26+
]

0 commit comments

Comments
 (0)