Skip to content
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

add solution for count-triplets #44

Merged
merged 1 commit into from
Oct 7, 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
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Algorithms (19/ 167)
## Algorithms (20/ 167)

| Name | Tags | Solution |
| --------------------------------------------------------- | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
Expand All @@ -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` | |
Expand Down
14 changes: 14 additions & 0 deletions src/algorithms/hashtables/count-triplets/count-triplets.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
34 changes: 34 additions & 0 deletions src/algorithms/hashtables/count-triplets/count-triplets.ts
Original file line number Diff line number Diff line change
@@ -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<number, number>()
const pairs = new Map<number, number>()

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
}