From a525def40d0c16d52b377e52ae612a5aba8cd468 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Fri, 19 Jul 2024 01:05:06 -0400 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Dictionaries=20and=20Hashmaps:=20Count=20Triplets.=20Solved?= =?UTF-8?q?=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../count_triplets_1_bruteforce.js | 26 ++++++++++ .../count_triplets_1_bruteforce.test.js | 47 ++++++++++++++++++ .../count_triplets_1_optimized.test.js | 49 +++++++++++++++++++ .../count_triplets_1_optmized.js | 38 ++++++++++++++ .../count_triplets_1_testcases.json | 26 ++++++++++ 5 files changed, 186 insertions(+) create mode 100644 src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.js create mode 100644 src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.js create mode 100644 src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.js create mode 100644 src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.js create mode 100644 src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_testcases.json diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.js b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.js new file mode 100644 index 00000000..edca0608 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.js @@ -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 }; diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.js b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.js new file mode 100644 index 00000000..590330ef --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.js @@ -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); + }); + }); +}); diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.js b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.js new file mode 100644 index 00000000..78890b9e --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.js @@ -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); + }); + }); +}); diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.js b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.js new file mode 100644 index 00000000..bef443a1 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.js @@ -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 }; diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_testcases.json b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_testcases.json new file mode 100644 index 00000000..a2df2410 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_testcases.json @@ -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 + } +]