Skip to content

Commit f0e6107

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Count Triplets. Brute force solution added.
1 parent 5862d62 commit f0e6107

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
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+
}

0 commit comments

Comments
 (0)