Skip to content

[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: C… #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md]]
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md]]
*/
import { logger as console } from '../../../logger.js';

export function countTriplets(arr, ratio) {
const size = arr.length;
let counter = 0;

for (let i = 0; i < size - 2; i++) {
for (let j = i + 1; j < size - 1; j++) {
for (let k = j + 1; k < size; k++) {
console.debug(`${arr[i]}, ${arr[j]}, ${arr[k]}`);

if (ratio * arr[i] === arr[j] && ratio * arr[j] === arr[k]) {
counter += 1;
}
}
}
}

return counter;
}

export default { countTriplets };
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger.js';

import { countTriplets } from './count_triplets_1_bruteforce.js';

const SMALL_TEST_CASES = [
{
title: 'Sample Test Case 0',
input: [1, 2, 2, 4],
r: 2,
expected: 2
},
{
title: 'Sample Test Case 1',
input: [1, 3, 9, 9, 27, 81],
r: 3,
expected: 6
},
{
title: 'Sample Test Case 1 (unsorted)',
input: [9, 3, 1, 81, 9, 27],
r: 3,
expected: 1
},
{
title: 'Sample Test Case 12',
input: [1, 5, 5, 25, 125],
r: 5,
expected: 4
}
];

describe('count_triplets_1', () => {
it('countTriplets test cases', () => {
expect.assertions(4);

SMALL_TEST_CASES.forEach((test) => {
const answer = countTriplets(test.input, test.r);

console.debug(
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
);

expect(answer).toStrictEqual(test.expected);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger.js';

import { countTriplets } from './count_triplets_1_optmized.js';
import SMALL_TEST_CASES from './count_triplets_1_testcases.json';

const BIG_TEST_CASES = [
{
title: 'Sample Test Case 2',
input: [
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,
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,
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,
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
],
r: 1,
expected: 161700
}
];

describe('count_triplets_1 (optimized)', () => {
it('countTriplets small test cases', () => {
expect.assertions(4);

SMALL_TEST_CASES.forEach((test) => {
const answer = countTriplets(test.input, test.r);

console.debug(
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
);

expect(answer).toStrictEqual(test.expected);
});
});

it('countTriplets big test cases', () => {
expect.assertions(1);

BIG_TEST_CASES.forEach((test) => {
const answer = countTriplets(test.input, test.r);

console.debug(
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
);

expect(answer).toStrictEqual(test.expected);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md]]
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md]]
*/

export function countTriplets(arr, ratio) {
let triplets = 0;

const aCounter = arr.reduce((accumulator, entry) => {
if (entry in accumulator) {
accumulator[entry] += 1;
} else {
accumulator[entry] = 1;
}
return accumulator;
}, {});

const bCounter = {};

arr.forEach((x) => {
const j = Math.floor(x / ratio);
const k = x * ratio;
aCounter[x] -= 1;
if (bCounter[j] && aCounter[k] && x % ratio === 0) {
triplets += bCounter[j] * aCounter[k];
}

if (x in bCounter) {
bCounter[x] += 1;
} else {
bCounter[x] = 1;
}
});

return triplets;
}

export default { countTriplets };
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"title": "Sample Test Case 0",
"input": [1, 2, 2, 4],
"r": 2,
"expected": 2
},
{
"title": "Sample Test Case 1",
"input": [1, 3, 9, 9, 27, 81],
"r": 3,
"expected": 6
},
{
"title": "Sample Test Case 1 (unsorted)",
"input": [9, 3, 1, 81, 9, 27],
"r": 3,
"expected": 1
},
{
"title": "Sample Test Case 12",
"input": [1, 5, 5, 25, 125],
"r": 5,
"expected": 4
}
]