From a23abb1240b6bd1466b3e7bed88365226ac46cd2 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Fri, 4 Jul 2025 00:43:17 -0400 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Greedy=20Algorithms:=20Max=20Min.=20Solved=20=E2=9C=93.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../greedy_algorithms/angry-children.md | 143 ++++++++++++++++++ .../greedy_algorithms/AngryFlorist.cs | 33 ++++ .../angry_children.testcases.json | 26 ++++ .../greedy_algorithms/AngryFloristTest.cs | 50 ++++++ 4 files changed, 252 insertions(+) create mode 100644 docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md create mode 100644 src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFlorist.cs create mode 100644 src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.testcases.json create mode 100644 src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFloristTest.cs diff --git a/docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md b/docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md new file mode 100644 index 0000000..1177d8e --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md @@ -0,0 +1,143 @@ +# [Greedy Algorithms: Max Min](https://www.hackerrank.com/challenges/angry-children) + +- Difficulty: `#medium` +- Category: `#ProblemSolvingBasic` `#greedyalgorithms` + +You will be given a list of integers, `arr`, and a single integer `k`. +You must create an array of length `k` from elements of `arr` such that +its unfairness is minimized. +Call that array `arr'`. +Unfairness of an array is calculated as + +$$ +\textsf{\textbf{max(arr')}} - \textsf{\textbf{min(arr')}} +$$ + +Where: + +- max denotes the largest integer in `arr'`. +- min denotes the smallest integer in `arr'`. + +## Example + +`arr = [1, 4, 7, 2]` +`k = 2` + +Pick any two elements, say `arr' = [4, 7]`. + +$ \textsf{\textbf{unfairness}} + = + \textsf{\textbf{max(4, 7)}} + - + \textsf{\textbf{min(4, 7)}} + = 7 - 4 = 3 +$ + +Testing for all pairs, the solution [1, 2] provides the minimum unfairness. + +**Note**: Integers in `arr` may not be unique. + +## Function Description + +Complete the maxMin function in the editor below. +maxMin has the following parameter(s): + +- `int k`: the number of elements to select +- `int arr[n]`: an array of integers + +## Returns + +- int: the minimum possible unfairness + +## Input Format + +The first line contains an integer , the number of elements in array . +The second line contains an integer . +Each of the next lines contains an integer where . + +## Constraints + +- $ 2 \leq n \leq 10^5 $ +- $ 2 \leq k \leq n $ +- $ 0 \leq arr[i] \leq 10^9 $ + +## Sample Input 0 + +```text +7 +3 +10 +100 +300 +200 +1000 +20 +30 +``` + +## Sample Output 0 + +```text +20 +``` + +## Explanation 0 + +Here `k = 3`; selecting the `3` integers `10, 20,30`, unfairness equals + +```text +max(10,20,30) - min(10,20,30) = 30 - 10 = 20 +``` + +## Sample Input 1 + +```text +10 +4 +1 +2 +3 +4 +10 +20 +30 +40 +100 +200 +``` + +## Sample Output 1 + +```text +3 +``` + +## Explanation 1 + +Here `k = 4`; selecting the `4` integers `1, 2, 3, 4`, unfairness equals + +```text +max(1,2,3,4) - min(1,2,3,4) = 4 - 1 = 3 +``` + +## Sample Input 2 + +```text +5 +2 +1 +2 +1 +2 +1 +``` + +## Sample Output 2 + +```text +0 +``` + +## Explanation 2 + +Here `k = 2`. `arr' = [2, 2]` or `arr' = [1, 1]` give the minimum unfairness of `0`. diff --git a/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFlorist.cs b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFlorist.cs new file mode 100644 index 0000000..4412feb --- /dev/null +++ b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFlorist.cs @@ -0,0 +1,33 @@ +// @link Problem definition [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md]] + +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.greedy_algorithms; + +using System.Diagnostics.CodeAnalysis; + + +/** + * AngryFlorist. + * + */ +public static class AngryFlorist +{ + /** + * maxMin. + */ + + public static int maxMin(int k, List arr) + { + List sortedlist = new List(arr); + sortedlist.Sort(); + int result = sortedlist[sortedlist.Count - 1] - sortedlist[0]; + + for (int i = 0; i < sortedlist.Count - k + 1; i++) + { + int tmin = sortedlist[i]; + int tmax = sortedlist[i + k - 1]; + result = Math.Min(result, tmax - tmin); + } + + return result; + } +} diff --git a/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.testcases.json b/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.testcases.json new file mode 100644 index 0000000..fe259d7 --- /dev/null +++ b/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.testcases.json @@ -0,0 +1,26 @@ +[ + { + "title": "Sample Test case 0", + "k": 3, + "arr": [10, 100, 300, 200, 1000, 20, 30], + "expected": 20 + }, + { + "title": "Sample Test case 1", + "k": 4, + "arr": [1, 2, 3, 4, 10, 20, 30, 40, 100, 200], + "expected": 3 + }, + { + "title": "Sample Test case 2", + "k": 2, + "arr": [1, 2, 1, 2, 1], + "expected": 0 + }, + { + "title": "Sample Test case 16", + "k": 3, + "arr": [100, 200, 300, 350, 400, 401, 402], + "expected": 2 + } +] diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFloristTest.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFloristTest.cs new file mode 100644 index 0000000..baa50b8 --- /dev/null +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFloristTest.cs @@ -0,0 +1,50 @@ +namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.greedy_algorithms; + +using algorithm_exercises_csharp_test.common; +using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.greedy_algorithms; + +/** + * AngryFloristTest. + */ +[TestClass] +public class AngryFloristTest +{ + public class AngryFloristTestCase(string title, int k, List arr, int expected) + { + public string Title { get; } = title; + public int K { get; } = k; + public List Arr { get; } = arr; + public int Expected { get; } = expected; + } + + private List testCases { get; set; } = default!; + + [TestInitialize] + public void testInitialize() + { + testCases = JsonLoader.resourceLoad>( + "hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.testcases.json" + ) ?? []; + } + + [TestMethod] + public void testLuckBalance() + { + foreach (AngryFloristTestCase test in testCases) + { + int result = AngryFlorist.maxMin(test.K, test.Arr); + + Assert.AreEqual( + test.Expected, + result, + string.Format( + System.Globalization.CultureInfo.InvariantCulture, + "AngryFlorist.maxMin({0}, {1}) => must be: {2}", + test.K, + test.Arr.ToString(), + test.Expected + ) + ); + } + } +}