From b2c1325cafec75053950a94ef87dd92fecda4624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A4ghib=20Hasan?= Date: Mon, 7 Oct 2024 20:55:16 +0300 Subject: [PATCH] add solution for count-triplets --- readme.md | 4 +-- .../count-triplets/count-triplets.spec.ts | 14 ++++++++ .../count-triplets/count-triplets.ts | 34 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/algorithms/hashtables/count-triplets/count-triplets.spec.ts create mode 100644 src/algorithms/hashtables/count-triplets/count-triplets.ts diff --git a/readme.md b/readme.md index 01071e6..f7b6325 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -## Algorithms (19/ 167) +## Algorithms (20/ 167) | Name | Tags | Solution | | --------------------------------------------------------- | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | @@ -11,7 +11,7 @@ | Ransom Note | `Hash Tables` | [TypeScript](./src/algorithms/hashtables/ransom-note) | | Two Strings | `Hash Tables` | [TypeScript](./src/algorithms/hashtables/two-strings) | | Sherlock and Anagrams | `Hash Tables` | [TypeScript](./src/algorithms/hashtables/sherlock-and-anagrams) | -| Count Triplets | `Hash Tables` | | +| Count Triplets | `Hash Tables` | [TypeScript](./src/algorithms/hashtables/count-triplets) | | Frequency Queries | `Hash Tables` | | | Making Anagrams | `Strings` | [TypeScript](./src/algorithms/strings/make-anagram) | | Alternating Characters | `Strings` | | diff --git a/src/algorithms/hashtables/count-triplets/count-triplets.spec.ts b/src/algorithms/hashtables/count-triplets/count-triplets.spec.ts new file mode 100644 index 0000000..4bcae05 --- /dev/null +++ b/src/algorithms/hashtables/count-triplets/count-triplets.spec.ts @@ -0,0 +1,14 @@ +import { countTriplets } from './count-triplets' + +describe('Count triplets', () => { + it('should return the number of triplets', () => { + expect(countTriplets([1, 2, 2, 4], 2)).toBe(2) + expect(countTriplets([1, 3, 9, 9, 27, 81], 3)).toBe(6) + expect(countTriplets([1, 5, 5, 25, 125], 5)).toBe(4) + expect(countTriplets([1, 1, 1, 1, 1], 1)).toBe(10) + }) + + it('should return 0 if there are no triplets', () => { + expect(countTriplets([1, 2, 3], 1)).toBe(0) + }) +}) diff --git a/src/algorithms/hashtables/count-triplets/count-triplets.ts b/src/algorithms/hashtables/count-triplets/count-triplets.ts new file mode 100644 index 0000000..9237810 --- /dev/null +++ b/src/algorithms/hashtables/count-triplets/count-triplets.ts @@ -0,0 +1,34 @@ +/** + * Count Triplets + * + * Given an array, find the number of triplets of incides (i, j, k) such that the elements at those indeces are in geometric progression + * for a given common ratio r and i < j < k + * + * arr = [1, 4, 16, 64] r = 4 + * + * [1, 4, 16] and [4, 16, 64] at indices (0, 1, 2) and (1, 2, 3) return 2 + * + * + * */ +export function countTriplets(arr: number[], r: number): number { + let count = 0 + const map = new Map() + const pairs = new Map() + + for (let i = 0; i < arr.length; i++) { + const num = arr[i] + + if (map.has(num)) { + count += map.get(num) || 0 + } + + if (pairs.has(num)) { + const pair = pairs.get(num) || 0 + map.set(num * r, (map.get(num * r) || 0) + pair) + } + + pairs.set(num * r, (pairs.get(num * r) || 0) + 1) + } + + return count +}