Skip to content

Commit 92fa861

Browse files
author
Gonzalo Diaz
committed
WIP
1 parent 89e7087 commit 92fa861

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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, Integer> a_counter = new HashMap<>();
25+
Map<Long, Integer> b_counter = new HashMap<>();
26+
Long triplets = 0L;
27+
28+
for (Long item : arr) {
29+
if (a_counter.putIfAbsent(item, 1) != null) {
30+
Integer currentValue = a_counter.get(item);
31+
a_counter.put(item, currentValue + 1);
32+
}
33+
}
34+
35+
for (Long item : arr) {
36+
Long j = item / r;
37+
Long k = item * r;
38+
a_counter.put(item, a_counter.get(item) - 1);
39+
if (b_counter.containsKey(j) && a_counter.containsKey(k) && item % r == 0) {
40+
triplets += b_counter.get(j) * a_counter.get(k);
41+
}
42+
43+
Integer b_item = b_counter.get(item);
44+
45+
if (b_item != null) {
46+
b_counter.put(item, b_item + 1);
47+
} else {
48+
b_counter.put(item, 1);
49+
}
50+
}
51+
52+
return triplets;
53+
}
54+
}
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+
public 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)