diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTriplets.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTriplets.java index b21e553..60ce2ff 100644 --- a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTriplets.java +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTriplets.java @@ -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() { diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTripletsBruteForce.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTripletsBruteForce.java new file mode 100644 index 0000000..a05ac79 --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTripletsBruteForce.java @@ -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 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; + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTripletsBruteForceTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTripletsBruteForceTest.java new file mode 100644 index 0000000..c2616c0 --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTripletsBruteForceTest.java @@ -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 input; + public Long r; + public Long expected; + } + + private List 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)); + } + } +} diff --git a/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md b/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md new file mode 100644 index 0000000..7c3d0a0 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md @@ -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/) diff --git a/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md b/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md new file mode 100644 index 0000000..53f95a8 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md @@ -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)