-
-
Notifications
You must be signed in to change notification settings - Fork 0
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: C... #193
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
...ew_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/) |
99 changes: 99 additions & 0 deletions
...kerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
44 changes: 44 additions & 0 deletions
...es_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTriplets.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // @link Problem definition [[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]] | ||
|
|
||
| namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps; | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| public class CountTriplets | ||
| { | ||
| public static long countTriplets(List<long> arr, long r) | ||
| { | ||
| Dictionary<long, long> aCounter = []; | ||
| Dictionary<long, long> bCounter = []; | ||
| long triplets = 0L; | ||
|
|
||
| foreach (long item in arr) | ||
| { | ||
| aCounter[item] = aCounter.TryGetValue(item, out long value) ? value + 1L : 1L; | ||
| } | ||
|
|
||
| long prevItemCount; | ||
| long nextItemCount; | ||
|
|
||
| foreach (long item in arr) | ||
| { | ||
| long j = item / r; | ||
| long k = item * r; | ||
|
|
||
| aCounter[item] = aCounter[item] - 1L; | ||
|
|
||
| prevItemCount = bCounter.TryGetValue(j, out long bItem) ? bItem : 0L; | ||
| nextItemCount = aCounter.TryGetValue(k, out long aItem) ? aItem : 0L; | ||
| if (item % r == 0) | ||
| { | ||
| triplets += prevItemCount * nextItemCount; | ||
| } | ||
|
|
||
| bCounter[item] = bCounter.TryGetValue(item, out long currentItemCount) ? currentItemCount + 1L : 1L; | ||
| } | ||
|
|
||
| return triplets; | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
...hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTripletsBruteForce.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md]] | ||
|
|
||
| namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps; | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Collections.Generic; | ||
|
|
||
| public class CountTripletsBruteForce | ||
| { | ||
| public static long countTriplets(List<long> arr, long r) | ||
| { | ||
| long size = arr.Count; | ||
| 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[i] == arr[j] && r * arr[j] == arr[k]) | ||
| { | ||
| counter += 1; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return counter; | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
...k/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.big.testcases.json
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
...interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.small.testcases.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| ] |
48 changes: 48 additions & 0 deletions
...test/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTriplets.Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps; | ||
| using algorithm_exercises_csharp_test.lib; | ||
| using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps; | ||
|
|
||
| [TestClass] | ||
| public class CountTripletsTest | ||
| { | ||
| public class CountTripletsTestCase | ||
| { | ||
| public string title { get; set; } = default!; | ||
| public List<long> input { get; set; } = default!; | ||
| public int r { get; set; } = default!; | ||
| public long expected { get; set; } = default!; | ||
| } | ||
|
|
||
| private List<CountTripletsTestCase> testCases { get; set; } = default!; | ||
| private List<CountTripletsTestCase> bigTestCases { get; set; } = default!; | ||
|
|
||
| [TestInitialize] | ||
| public void testInitialize() | ||
| { | ||
| testCases = JsonLoader.resourceLoad<List<CountTripletsTestCase>>( | ||
| "hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.small.testcases.json" | ||
| ) ?? []; | ||
|
|
||
| bigTestCases = JsonLoader.resourceLoad<List<CountTripletsTestCase>>( | ||
| "hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.big.testcases.json" | ||
| ) ?? []; | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void testCountTriplets() | ||
| { | ||
| long result; | ||
|
|
||
| foreach (CountTripletsTestCase test in testCases) | ||
| { | ||
| result = CountTriplets.countTriplets(test.input, test.r); | ||
| Assert.AreEqual(test.expected, result); | ||
| } | ||
|
|
||
| foreach (CountTripletsTestCase test in bigTestCases) | ||
| { | ||
| result = CountTriplets.countTriplets(test.input, test.r); | ||
| Assert.AreEqual(test.expected, result); | ||
| } | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
...rrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTripletsBruteForce.Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps; | ||
| using algorithm_exercises_csharp_test.lib; | ||
| using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps; | ||
|
|
||
| [TestClass] | ||
| public class CountTripletsBruteForceTest | ||
| { | ||
| public class CountTripletsBruteForceTestCase | ||
| { | ||
| public string title { get; set; } = default!; | ||
| public List<long> input { get; set; } = default!; | ||
| public int r { get; set; } = default!; | ||
| public int expected { get; set; } = default!; | ||
| } | ||
|
|
||
| private List<CountTripletsBruteForceTestCase> testCases { get; set; } = default!; | ||
|
|
||
| [TestInitialize] | ||
| public void testInitialize() | ||
| { | ||
| testCases = JsonLoader.resourceLoad<List<CountTripletsBruteForceTestCase>>( | ||
| "hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.small.testcases.json" | ||
| ) ?? []; | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void testCountTriplets() | ||
| { | ||
| long result; | ||
|
|
||
| foreach (CountTripletsBruteForceTestCase test in testCases) | ||
| { | ||
| result = CountTripletsBruteForce.countTriplets(test.input, test.r); | ||
| Assert.AreEqual(test.expected, result); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.