diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArray.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArray.java new file mode 100644 index 0000000..a4cc3e1 --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArray.java @@ -0,0 +1,45 @@ +package ae.hackerrank.interview_preparation_kit.greedy_algorithms; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * MinimumAbsoluteDifferenceInAnArray. + * + * @link Problem definition + * [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/minimum-absolute-difference-in-an-array.md]] + */ +public class MinimumAbsoluteDifferenceInAnArray { + + private MinimumAbsoluteDifferenceInAnArray() { + } + + /** + * minimumAbsoluteDifference. + */ + public static int minimumAbsoluteDifference(List arr) { + List sortedNums = arr.stream().collect(Collectors.toList()); + Collections.sort(sortedNums); + + // Find the minimum absolute difference + int result = 0; + boolean resultEmpty = true; + + for (int i = 0; i < sortedNums.size() - 1; i++) { + int aValue = sortedNums.get(i); + int bValue = sortedNums.get(i + 1); + + int diff = Math.abs(aValue - bValue); + + if (resultEmpty) { + result = diff; + resultEmpty = false; + } else { + result = Math.min(result, diff); + } + } + + return result; + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArrayTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArrayTest.java new file mode 100644 index 0000000..2f1a528 --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArrayTest.java @@ -0,0 +1,48 @@ +package ae.hackerrank.interview_preparation_kit.greedy_algorithms; + +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; + +/** + * MinimumAbsoluteDifferenceInAnArrayTest. + */ +@TestInstance(Lifecycle.PER_CLASS) +class MinimumAbsoluteDifferenceInAnArrayTest { + public static class MinimumAbsoluteDifferenceInAnArrayTestCase { + public String title; + public List input; + public Integer expected; + } + + private List testCases; + + @BeforeAll + void setup() throws IOException { + String path = String.join("/", + "hackerrank", + "interview_preparation_kit", + "greedy_algorithms", + "minimum_absolute_difference_in_an_array.testcases.json"); + this.testCases = JsonLoader.loadJson(path, MinimumAbsoluteDifferenceInAnArrayTestCase.class); + } + + @Test + void testLuckBalance() { + for (MinimumAbsoluteDifferenceInAnArrayTestCase test : testCases) { + Integer result = MinimumAbsoluteDifferenceInAnArray.minimumAbsoluteDifference(test.input); + + assertEquals(test.expected, result, + "%s(%s) => must be: %d".formatted( + "MinimumAbsoluteDifferenceInAnArray.minimumAbsoluteDifference", + test.input.toString(), + test.expected)); + } + } +} diff --git a/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/greedy_algorithms/minimum_absolute_difference_in_an_array.testcases.json b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/greedy_algorithms/minimum_absolute_difference_in_an_array.testcases.json new file mode 100644 index 0000000..2bcb356 --- /dev/null +++ b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/greedy_algorithms/minimum_absolute_difference_in_an_array.testcases.json @@ -0,0 +1,17 @@ +[ + { + "title": "Sample Test case 0", + "input": [3, -7, 0], + "expected": 3 + }, + { + "title": "Sample Test case 1", + "input": [-59, -36, -13, 1, -53, -92, -2, -96, -54, 75], + "expected": 1 + }, + { + "title": "Sample Test case 2", + "input": [1, -3, 71, 68, 17], + "expected": 3 + } +] diff --git a/docs/hackerrank/interview_preparation_kit/greedy_algorithms/minimum-absolute-difference-in-an-array.md b/docs/hackerrank/interview_preparation_kit/greedy_algorithms/minimum-absolute-difference-in-an-array.md new file mode 100644 index 0000000..63a3eaa --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/greedy_algorithms/minimum-absolute-difference-in-an-array.md @@ -0,0 +1,106 @@ +# [Minimum Absolute Difference in an Array](https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array) + +- Difficulty: `#medium` +- Category: `#ProblemSolvingBasic` `#greedyalgorithms` + +The absolute difference is the positive difference between two +values `a` and `b`, is written $ \lvert a - b \rvert $ +or $ \lvert b - a \rvert $ +and they are equal. If `a = 3` and `b = 2`, $ \lvert 3 - 2 \rvert = +\lvert 2 - 3 \rvert = 1 $. +Given an array of integers, find the minimum absolute difference +between any two elements in the array. + +**Example** `arr = [-2, 2, 4]`6 + +There are pairs of numbers: `[-2, 2]`, `[-2, 4]` and `[2, 4]`. +The absolute differences for these pairs are +$ \lvert (-2) - 2 \rvert = 4 $, $ \lvert (-2) - 4 \rvert = 6 $ and +$ \lvert 2 - 4 \rvert = 2 $. +The minimum absolute difference is `2`. + +## Function Description + +Complete the minimumAbsoluteDifference function in the editor below. +It should return an integer that represents the minimum absolute difference +between any pair of elements. + +minimumAbsoluteDifference has the following parameter(s): + +- `int arr[n]`: an array of integers + +## Returns + +int: the minimum absolute difference found + +## Input Format + +The first line contains a single integer , the size of . +The second line contains space-separated integers, . + +## Constraints + +- $ 2 \leq n \leq 10^5 $ +- $ -10^9 \leq arr[i] \leq 10^9 $ + +## Sample Input 0 + +```text +3 +3 -7 0 +``` + +## Sample Output 0 + +```text +3 +``` + +## Explanation 0 + +The first line of input is the number of array elements. The array, + `arr = [3, -7, 0]` +There are three pairs to test: `(3, -7)`, `(3, 0)`, and `(-7, 0)`. +The absolute differences are: + +- $ \lvert 3 - -7 \rvert => 10 $ +- $ \lvert 3 - 0 \rvert => 3 $ +- $ \lvert -7 - 0 \rvert => 7 $ + +Remember that the order of values in +the subtraction does not influence the result. +The smallest of these absolute differences is `3`. + +## Sample Input 1 + +```text +10 +-59 -36 -13 1 -53 -92 -2 -96 -54 75 +``` + +## Sample Output 1 + +```text +1 +``` + +## Explanation 1 + +The smallest absolute difference is $ \lvert - 54 - -53 \rvert = 1$. + +## Sample Input 2 + +```text +5 +1 -3 71 68 17 +``` + +## Sample Output 2 + +```text +3 +``` + +## Explanation 2 + +The minimum absolute difference is $ \lvert - 71 - 68 \rvert = 3$.