Skip to content

[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: C… #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* CountTriplets.
*
* @link Problem definition
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings.md]]
* @link Solution notes
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings-solution-notes.md]]
*/
public class CountTriplets {
private CountTriplets() {
}

/**
* CountTriplets.
*/
static long countTriplets(List<Long> arr, long r) {

Map<Long, Long> aCounter = new HashMap<>();
Map<Long, Long> bCounter = new HashMap<>();
long triplets = 0L;

for (Long item : arr) {
if (aCounter.putIfAbsent(item, 1L) != null) {
Long currentValue = aCounter.get(item);
aCounter.put(item, currentValue + 1L);
}
}

long jItem;
long kItem;

for (Long item : arr) {
Long j = item / r;
Long k = item * r;

aCounter.put(item, aCounter.get(item) - 1L);

jItem = bCounter.get(j) != null ? bCounter.get(j) : 0L;
kItem = aCounter.get(k) != null ? aCounter.get(k) : 0L;
if (item % r == 0) {
triplets += jItem * kItem;
}

Long bItem = bCounter.get(item);

bCounter.put(item, bItem != null ? bItem + 1L : 1L);
}

return triplets;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import util.JsonLoader;

/**
* CountTripletsTest.
*/
@TestInstance(Lifecycle.PER_CLASS)
class CountTripletsTest {
public static class CountTripletsTestCase {
public String title;
public List<Long> input;
public Long r;
public Long expected;
}

private List<CountTripletsTestCase> smallTestCases;
private List<CountTripletsTestCase> bigTestCases;

@BeforeAll
void setup() throws IOException {
String path;
path = String.join("/",
"hackerrank",
"interview_preparation_kit",
"dictionaries_and_hashmaps",
"count_triplets_1.small.testcases.json");

this.smallTestCases = JsonLoader.loadJson(path, CountTripletsTestCase.class);

path = String.join("/",
"hackerrank",
"interview_preparation_kit",
"dictionaries_and_hashmaps",
"count_triplets_1.big.testcases.json");

this.bigTestCases = JsonLoader.loadJson(path, CountTripletsTestCase.class);

}

@Test
void testCountTriplets() {
for (CountTripletsTestCase test : smallTestCases) {
Long solutionFound = CountTriplets.countTriplets(test.input, test.r);

assertEquals(test.expected, solutionFound,
"%s(%s, %d) answer must be: %d".formatted(
"CountTriplets.countTriplets",
test.input.toString(),
test.r,
test.expected));
}
}

@Test
void testCountTripletsBigCases() {
for (CountTripletsTestCase test : bigTestCases) {
Long solutionFound = CountTriplets.countTriplets(test.input, test.r);

assertEquals(test.expected, solutionFound,
"%s(%s, %d) answer must be: %d".formatted(
"CountTriplets.countTriplets",
test.input.toString(),
test.r,
test.expected));
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"title": "Sample Test Case 0",
"input": [1, 2, 2, 4],
"r": 2,
"expected": 2
},
{
"title": "Sample Test Case 1",
"input": [1, 3, 9, 9, 27, 81],
"r": 3,
"expected": 6
},
{
"title": "Sample Test Case 1 (unsorted)",
"input": [9, 3, 1, 81, 9, 27],
"r": 3,
"expected": 1
},
{
"title": "Sample Test Case 12",
"input": [1, 5, 5, 25, 125],
"r": 5,
"expected": 4
}
]