Skip to content

Commit 6b3eab3

Browse files
authored
Merge pull request #296 from sir-gon/feature/count_triplets
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: C…
2 parents 53977bd + 640ed69 commit 6b3eab3

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-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,76 @@
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+
28+
@BeforeAll
29+
void setup() throws IOException {
30+
String path;
31+
path = String.join("/",
32+
"hackerrank",
33+
"interview_preparation_kit",
34+
"dictionaries_and_hashmaps",
35+
"count_triplets_1.small.testcases.json");
36+
37+
this.smallTestCases = JsonLoader.loadJson(path, CountTripletsTestCase.class);
38+
39+
path = String.join("/",
40+
"hackerrank",
41+
"interview_preparation_kit",
42+
"dictionaries_and_hashmaps",
43+
"count_triplets_1.big.testcases.json");
44+
45+
this.bigTestCases = JsonLoader.loadJson(path, CountTripletsTestCase.class);
46+
47+
}
48+
49+
@Test
50+
void testCountTriplets() {
51+
for (CountTripletsTestCase test : smallTestCases) {
52+
Long solutionFound = CountTriplets.countTriplets(test.input, test.r);
53+
54+
assertEquals(test.expected, solutionFound,
55+
"%s(%s, %d) answer must be: %d".formatted(
56+
"CountTriplets.countTriplets",
57+
test.input.toString(),
58+
test.r,
59+
test.expected));
60+
}
61+
}
62+
63+
@Test
64+
void testCountTripletsBigCases() {
65+
for (CountTripletsTestCase test : bigTestCases) {
66+
Long solutionFound = CountTriplets.countTriplets(test.input, test.r);
67+
68+
assertEquals(test.expected, solutionFound,
69+
"%s(%s, %d) answer must be: %d".formatted(
70+
"CountTriplets.countTriplets",
71+
test.input.toString(),
72+
test.r,
73+
test.expected));
74+
}
75+
}
76+
}

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,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)