Skip to content

Commit feb835f

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

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
/**
8+
* CountTriplets.
9+
*
10+
* @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]]
14+
*/
15+
public class CountTriplets {
16+
private CountTriplets() {
17+
}
18+
19+
/**
20+
* CountTriplets.
21+
*/
22+
static long countTriplets(List<Long> arr, long r) {
23+
24+
Map<Long, Long> aCounter = new HashMap<>();
25+
Map<Long, Long> bCounter = new HashMap<>();
26+
long triplets = 0L;
27+
28+
for (Long item : arr) {
29+
if (aCounter.putIfAbsent(item, 1L) != null) {
30+
Long currentValue = aCounter.get(item);
31+
aCounter.put(item, currentValue + 1L);
32+
}
33+
}
34+
35+
long jItem;
36+
long kItem;
37+
38+
for (Long item : arr) {
39+
Long j = item / r;
40+
Long k = item * r;
41+
42+
aCounter.put(item, aCounter.get(item) - 1L);
43+
44+
jItem = bCounter.get(j) != null ? bCounter.get(j) : 0L;
45+
kItem = aCounter.get(k) != null ? aCounter.get(k) : 0L;
46+
if (item % r == 0) {
47+
triplets += jItem * kItem;
48+
}
49+
50+
Long bItem = bCounter.get(item);
51+
52+
bCounter.put(item, bItem != null ? bItem + 1L : 1L);
53+
}
54+
55+
return triplets;
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
* CountTripletsTest.
15+
*/
16+
@TestInstance(Lifecycle.PER_CLASS)
17+
class CountTripletsTest {
18+
public static class CountTripletsTestCase {
19+
public String title;
20+
public List<Long> input;
21+
public Long r;
22+
public Long expected;
23+
}
24+
25+
private List<CountTripletsTestCase> smallTestCases;
26+
private List<CountTripletsTestCase> bigTestCases;
27+
private List<CountTripletsTestCase> borderTestCases;
28+
29+
@BeforeAll
30+
void setup() throws IOException {
31+
String path;
32+
path = String.join("/",
33+
"hackerrank",
34+
"interview_preparation_kit",
35+
"dictionaries_and_hashmaps",
36+
"count_triplets_1.small.testcases.json");
37+
38+
this.smallTestCases = JsonLoader.loadJson(path, CountTripletsTestCase.class);
39+
40+
path = String.join("/",
41+
"hackerrank",
42+
"interview_preparation_kit",
43+
"dictionaries_and_hashmaps",
44+
"count_triplets_1.big.testcases.json");
45+
46+
this.bigTestCases = JsonLoader.loadJson(path, CountTripletsTestCase.class);
47+
48+
path = String.join("/",
49+
"hackerrank",
50+
"interview_preparation_kit",
51+
"dictionaries_and_hashmaps",
52+
"count_triplets_1.border.testcases.json");
53+
54+
this.borderTestCases = JsonLoader.loadJson(path, CountTripletsTestCase.class);
55+
56+
}
57+
58+
@Test
59+
void testCountTriplets() {
60+
for (CountTripletsTestCase test : smallTestCases) {
61+
Long solutionFound = CountTriplets.countTriplets(test.input, test.r);
62+
63+
assertEquals(test.expected, solutionFound,
64+
"%s(%s, %d) answer must be: %d".formatted(
65+
"CountTriplets.countTriplets",
66+
test.input.toString(),
67+
test.r,
68+
test.expected));
69+
}
70+
}
71+
72+
@Test
73+
void testCountTripletsBigCases() {
74+
for (CountTripletsTestCase test : bigTestCases) {
75+
Long solutionFound = CountTriplets.countTriplets(test.input, test.r);
76+
77+
assertEquals(test.expected, solutionFound,
78+
"%s(%s, %d) answer must be: %d".formatted(
79+
"CountTriplets.countTriplets",
80+
test.input.toString(),
81+
test.r,
82+
test.expected));
83+
}
84+
}
85+
}

algorithm-exercises-java/src/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,8 @@
1+
[
2+
{
3+
"title": "Border test case 0",
4+
"input": [1, 2, 3, 4],
5+
"r": 2,
6+
"expected": 1
7+
}
8+
]
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+
]

0 commit comments

Comments
 (0)