Skip to content

Commit e551e86

Browse files
authored
Merge pull request #298 from sir-gon/feature/count_triplets
Feature/count triplets
2 parents ede4aea + f0e6107 commit e551e86

File tree

5 files changed

+206
-3
lines changed

5 files changed

+206
-3
lines changed

algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTriplets.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
* CountTriplets.
99
*
1010
* @link Problem definition
11-
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings.md]]
12-
* @link Solution notes
13-
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings-solution-notes.md]]
11+
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md/]]
12+
*
13+
* @link Solution Notes
14+
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md/]]
1415
*/
1516
public class CountTriplets {
1617
private CountTriplets() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
2+
3+
import java.util.List;
4+
5+
/**
6+
* CountTripletsBruteForce.
7+
*
8+
* @link Problem definition
9+
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md/]]
10+
*
11+
* @see CountTriplets
12+
*/
13+
public class CountTripletsBruteForce {
14+
private CountTripletsBruteForce() {
15+
}
16+
17+
/**
18+
* CountTriples.
19+
*/
20+
static long countTriplets(List<Long> arr, long r) {
21+
22+
long size = arr.size();
23+
long counter = 0L;
24+
25+
for (int i = 0; i < size - 2; i++) {
26+
for (int j = i + 1; j < size - 1; j++) {
27+
for (int k = j + 1; k < size; k++) {
28+
29+
if (r * arr.get(i) == arr.get(j) && r * arr.get(j) == arr.get(k)) {
30+
counter += 1;
31+
}
32+
}
33+
}
34+
}
35+
36+
return counter;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
import util.JsonLoader;
12+
13+
/**
14+
* CountTripletsBruteForceTest.
15+
*/
16+
@TestInstance(Lifecycle.PER_CLASS)
17+
class CountTripletsBruteForceTest {
18+
public static class CountTripletsBruteForceTestCase {
19+
public String title;
20+
public List<Long> input;
21+
public Long r;
22+
public Long expected;
23+
}
24+
25+
private List<CountTripletsBruteForceTestCase> smallTestCases;
26+
27+
@BeforeAll
28+
void setup() throws IOException {
29+
String path;
30+
path = String.join("/",
31+
"hackerrank",
32+
"interview_preparation_kit",
33+
"dictionaries_and_hashmaps",
34+
"count_triplets_1.small.testcases.json");
35+
36+
this.smallTestCases = JsonLoader.loadJson(path, CountTripletsBruteForceTestCase.class);
37+
}
38+
39+
@Test
40+
void testCountTriplets() {
41+
for (CountTripletsBruteForceTestCase test : smallTestCases) {
42+
Long solutionFound = CountTripletsBruteForce.countTriplets(test.input, test.r);
43+
44+
assertEquals(test.expected, solutionFound,
45+
"%s(%s, %d) answer must be: %d".formatted(
46+
"CountTripletsBruteForce.countTriplets",
47+
test.input.toString(),
48+
test.r,
49+
test.expected));
50+
}
51+
}
52+
}
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)

0 commit comments

Comments
 (0)