Skip to content

Feature/count triplets #298

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 2 commits 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
Expand Up @@ -8,9 +8,10 @@
* 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]]
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md/]]
*
* @link Solution Notes
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md/]]
*/
public class CountTriplets {
private CountTriplets() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;

import java.util.List;

/**
* CountTripletsBruteForce.
*
* @link Problem definition
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md/]]
*
* @see CountTriplets
*/
public class CountTripletsBruteForce {
private CountTripletsBruteForce() {
}

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

long size = arr.size();
long counter = 0L;

for (int i = 0; i < size - 2; i++) {
for (int j = i + 1; j < size - 1; j++) {
for (int k = j + 1; k < size; k++) {

if (r * arr.get(i) == arr.get(j) && r * arr.get(j) == arr.get(k)) {
counter += 1;
}
}
}
}

return counter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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;

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

private List<CountTripletsBruteForceTestCase> smallTestCases;

@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, CountTripletsBruteForceTestCase.class);
}

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

assertEquals(test.expected, solutionFound,
"%s(%s, %d) answer must be: %d".formatted(
"CountTripletsBruteForce.countTriplets",
test.input.toString(),
test.r,
test.expected));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# [Dictionaries and Hashmaps: Count Triplets](https://www.hackerrank.com/challenges/count-triplets-1)

- Difficulty: `#medium`
- Category: `#ProblemSolvingIntermediate`

## Working solution

This solution in O(N), is based on considering that each
number analyzed is in the center of the triplet and asks
"how many" possible cases there are on the left and
right to calculate how many possible triplets are formed.

- Source: [Hackerrank - Count Triplets Solution](https://www.thepoorcoder.com/hackerrank-count-triplets-solution/)
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# [Dictionaries and Hashmaps: Count Triplets](https://www.hackerrank.com/challenges/count-triplets-1)

- Difficulty: `#medium`
- Category: `#ProblemSolvingIntermediate`

You are given an array and you need to find number of
tripets of indices (i, j, k) such that the elements at
those indices are in geometric progression for a given
common ratio `r` and $ i < j < k $.

## Example

`arr = [1, 4, 16, 64] r = 4`

There are `[1, 4, 16]` and `[4, 16, 64]` at indices (0, 1, 2) and (1, 2, 3).
Return `2`.

## Function Description

Complete the countTriplets function in the editor below.

countTriplets has the following parameter(s):

- `int arr[n]`: an array of integers
- `int r`: the common ratio

## Returns

- `int`: the number of triplets

## Input Format

The first line contains two space-separated integers `n` and `r`,
the size of `arr` and the common ratio.
The next line contains `n` space-seperated integers `arr[i]`.

## Constraints

- $ 1 \leq n \leq 10^5 $
- $ 1 \leq r \leq 10^9 $
- $ 1 \leq arr[i] \leq 10^9 $

## Sample Input 0

```text
4 2
1 2 2 4
```

## Sample Output 0

```text
2
```

## Explanation 0

There are `2` triplets in satisfying our criteria,
whose indices are (0, 1, 3) and (0, 2, 3)

## Sample Input 1

```text
6 3
1 3 9 9 27 81
```

## Sample Output 1

```text
6
```

## Explanation 1

The triplets satisfying are index
`(0, 1, 2)`, `(0, 1, 3)`, `(1, 2, 4)`, `(1, 3, 4)`, `(2, 4, 5)` and `(3, 4, 5)`.

## Sample Input 2

```text
5 5
1 5 5 25 125
```

## Sample Output 2

```text
4
```

## Explanation 2

The triplets satisfying are index
`(0, 1, 3)`, `(0, 2, 3)`, `(1, 2, 3)`, `(2, 3, 4)`.

## Appendix

[Solution notes](count_triplets_1-solution-notes.md)