Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0cc5b42

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Count Triplets. Solved ✓.
1 parent 0978443 commit 0cc5b42

File tree

8 files changed

+308
-0
lines changed

8 files changed

+308
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# [Dictionaries and Hashmaps: Count Triplets](https://www.hackerrank.com/challenges/count-triplets-1)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingIntermediate`
5+
6+
## Working solution
7+
8+
This solution in O(N), is based on considering that each
9+
number analyzed is in the center of the triplet and asks
10+
"how many" possible cases there are on the left and
11+
right to calculate how many possible triplets are formed.
12+
13+
- Source: [Hackerrank - Count Triplets Solution](https://www.thepoorcoder.com/hackerrank-count-triplets-solution/)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# [Dictionaries and Hashmaps: Count Triplets](https://www.hackerrank.com/challenges/count-triplets-1)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingIntermediate`
5+
6+
You are given an array and you need to find number of
7+
tripets of indices (i, j, k) such that the elements at
8+
those indices are in geometric progression for a given
9+
common ratio `r` and $ i < j < k $.
10+
11+
## Example
12+
13+
`arr = [1, 4, 16, 64] r = 4`
14+
15+
There are `[1, 4, 16]` and `[4, 16, 64]` at indices (0, 1, 2) and (1, 2, 3).
16+
Return `2`.
17+
18+
## Function Description
19+
20+
Complete the countTriplets function in the editor below.
21+
22+
countTriplets has the following parameter(s):
23+
24+
- `int arr[n]`: an array of integers
25+
- `int r`: the common ratio
26+
27+
## Returns
28+
29+
- `int`: the number of triplets
30+
31+
## Input Format
32+
33+
The first line contains two space-separated integers `n` and `r`,
34+
the size of `arr` and the common ratio.
35+
The next line contains `n` space-seperated integers `arr[i]`.
36+
37+
## Constraints
38+
39+
- $ 1 \leq n \leq 10^5 $
40+
- $ 1 \leq r \leq 10^9 $
41+
- $ 1 \leq arr[i] \leq 10^9 $
42+
43+
## Sample Input 0
44+
45+
```text
46+
4 2
47+
1 2 2 4
48+
```
49+
50+
## Sample Output 0
51+
52+
```text
53+
2
54+
```
55+
56+
## Explanation 0
57+
58+
There are `2` triplets in satisfying our criteria,
59+
whose indices are (0, 1, 3) and (0, 2, 3)
60+
61+
## Sample Input 1
62+
63+
```text
64+
6 3
65+
1 3 9 9 27 81
66+
```
67+
68+
## Sample Output 1
69+
70+
```text
71+
6
72+
```
73+
74+
## Explanation 1
75+
76+
The triplets satisfying are index
77+
`(0, 1, 2)`, `(0, 1, 3)`, `(1, 2, 4)`, `(1, 3, 4)`, `(2, 4, 5)` and `(3, 4, 5)`.
78+
79+
## Sample Input 2
80+
81+
```text
82+
5 5
83+
1 5 5 25 125
84+
```
85+
86+
## Sample Output 2
87+
88+
```text
89+
4
90+
```
91+
92+
## Explanation 2
93+
94+
The triplets satisfying are index
95+
`(0, 1, 3)`, `(0, 2, 3)`, `(1, 2, 3)`, `(2, 3, 4)`.
96+
97+
## Appendix
98+
99+
[Solution notes](count_triplets_1-solution-notes.md)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md]]
2+
3+
// @link Solution notes [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md]]
4+
5+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
6+
7+
using System.Collections.Generic;
8+
9+
public class CountTriplets
10+
{
11+
public static long countTriplets(List<long> arr, long r)
12+
{
13+
Dictionary<long, long> aCounter = [];
14+
Dictionary<long, long> bCounter = [];
15+
long triplets = 0L;
16+
17+
foreach (long item in arr)
18+
{
19+
aCounter[item] = aCounter.TryGetValue(item, out long value) ? value + 1L : 1L;
20+
}
21+
22+
long prevItemCount;
23+
long nextItemCount;
24+
25+
foreach (long item in arr)
26+
{
27+
long j = item / r;
28+
long k = item * r;
29+
30+
aCounter[item] = aCounter[item] - 1L;
31+
32+
prevItemCount = bCounter.TryGetValue(j, out long bItem) ? bItem : 0L;
33+
nextItemCount = aCounter.TryGetValue(k, out long aItem) ? aItem : 0L;
34+
if (item % r == 0)
35+
{
36+
triplets += prevItemCount * nextItemCount;
37+
}
38+
39+
bCounter[item] = bCounter.TryGetValue(item, out long currentItemCount) ? currentItemCount + 1L : 1L;
40+
}
41+
42+
return triplets;
43+
}
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Collections.Generic;
7+
8+
public class CountTripletsBruteForce
9+
{
10+
public static long countTriplets(List<long> arr, long r)
11+
{
12+
long size = arr.Count;
13+
long counter = 0L;
14+
15+
for (int i = 0; i < size - 2; i++)
16+
{
17+
for (int j = i + 1; j < size - 1; j++)
18+
{
19+
for (int k = j + 1; k < size; k++)
20+
{
21+
22+
if (r * arr[i] == arr[j] && r * arr[j] == arr[k])
23+
{
24+
counter += 1;
25+
}
26+
}
27+
}
28+
}
29+
30+
return counter;
31+
}
32+
}

‎src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.big.testcases.json‎

Lines changed: 9 additions & 0 deletions
Large diffs are not rendered by default.
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+
]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
2+
using algorithm_exercises_csharp_test.lib;
3+
using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
4+
5+
[TestClass]
6+
public class CountTripletsTest
7+
{
8+
public class CountTripletsTestCase
9+
{
10+
public string title { get; set; } = default!;
11+
public List<long> input { get; set; } = default!;
12+
public int r { get; set; } = default!;
13+
public long expected { get; set; } = default!;
14+
}
15+
16+
private List<CountTripletsTestCase> testCases { get; set; } = default!;
17+
private List<CountTripletsTestCase> bigTestCases { get; set; } = default!;
18+
19+
[TestInitialize]
20+
public void testInitialize()
21+
{
22+
testCases = JsonLoader.resourceLoad<List<CountTripletsTestCase>>(
23+
"hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.small.testcases.json"
24+
) ?? [];
25+
26+
bigTestCases = JsonLoader.resourceLoad<List<CountTripletsTestCase>>(
27+
"hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.big.testcases.json"
28+
) ?? [];
29+
}
30+
31+
[TestMethod]
32+
public void testCountTriplets()
33+
{
34+
long result;
35+
36+
foreach (CountTripletsTestCase test in testCases)
37+
{
38+
result = CountTriplets.countTriplets(test.input, test.r);
39+
Assert.AreEqual(test.expected, result);
40+
}
41+
42+
foreach (CountTripletsTestCase test in bigTestCases)
43+
{
44+
result = CountTriplets.countTriplets(test.input, test.r);
45+
Assert.AreEqual(test.expected, result);
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
2+
using algorithm_exercises_csharp_test.lib;
3+
using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
4+
5+
[TestClass]
6+
public class CountTripletsBruteForceTest
7+
{
8+
public class CountTripletsBruteForceTestCase
9+
{
10+
public string title { get; set; } = default!;
11+
public List<long> input { get; set; } = default!;
12+
public int r { get; set; } = default!;
13+
public int expected { get; set; } = default!;
14+
}
15+
16+
private List<CountTripletsBruteForceTestCase> testCases { get; set; } = default!;
17+
18+
[TestInitialize]
19+
public void testInitialize()
20+
{
21+
testCases = JsonLoader.resourceLoad<List<CountTripletsBruteForceTestCase>>(
22+
"hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.small.testcases.json"
23+
) ?? [];
24+
}
25+
26+
[TestMethod]
27+
public void testCountTriplets()
28+
{
29+
long result;
30+
31+
foreach (CountTripletsBruteForceTestCase test in testCases)
32+
{
33+
result = CountTripletsBruteForce.countTriplets(test.input, test.r);
34+
Assert.AreEqual(test.expected, result);
35+
}
36+
}
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /