Skip to content

Commit ca0139e

Browse files
authored
Merge pull request #205 from sir-gon/feature/minimum-absolute-difference-in-an-array
[Hacker Rank] Interview Preparation Kit: Greedy Algorithms: Minimum A…
2 parents 4a0297b + fd9155d commit ca0139e

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# [Minimum Absolute Difference in an Array](https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingBasic` `#greedyalgorithms`
5+
6+
The absolute difference is the positive difference between two
7+
values `a` and `b`, is written $ \lvert a - b \rvert $
8+
or $ \lvert b - a \rvert $
9+
and they are equal. If `a = 3` and `b = 2`, $ \lvert 3 - 2 \rvert =
10+
\lvert 2 - 3 \rvert = 1 $.
11+
Given an array of integers, find the minimum absolute difference
12+
between any two elements in the array.
13+
14+
**Example** `arr = [-2, 2, 4]`6
15+
16+
There are pairs of numbers: `[-2, 2]`, `[-2, 4]` and `[2, 4]`.
17+
The absolute differences for these pairs are
18+
$ \lvert (-2) - 2 \rvert = 4 $, $ \lvert (-2) - 4 \rvert = 6 $ and
19+
$ \lvert 2 - 4 \rvert = 2 $.
20+
The minimum absolute difference is `2`.
21+
22+
## Function Description
23+
24+
Complete the minimumAbsoluteDifference function in the editor below.
25+
It should return an integer that represents the minimum absolute difference
26+
between any pair of elements.
27+
28+
minimumAbsoluteDifference has the following parameter(s):
29+
30+
- `int arr[n]`: an array of integers
31+
32+
## Returns
33+
34+
int: the minimum absolute difference found
35+
36+
## Input Format
37+
38+
The first line contains a single integer , the size of .
39+
The second line contains space-separated integers, .
40+
41+
## Constraints
42+
43+
- $ 2 \leq n \leq 10^5 $
44+
- $ -10^9 \leq arr[i] \leq 10^9 $
45+
46+
## Sample Input 0
47+
48+
```text
49+
3
50+
3 -7 0
51+
```
52+
53+
## Sample Output 0
54+
55+
```text
56+
3
57+
```
58+
59+
## Explanation 0
60+
61+
The first line of input is the number of array elements. The array,
62+
`arr = [3, -7, 0]`
63+
There are three pairs to test: `(3, -7)`, `(3, 0)`, and `(-7, 0)`.
64+
The absolute differences are:
65+
66+
- $ \lvert 3 - -7 \rvert => 10 $
67+
- $ \lvert 3 - 0 \rvert => 3 $
68+
- $ \lvert -7 - 0 \rvert => 7 $
69+
70+
Remember that the order of values in
71+
the subtraction does not influence the result.
72+
The smallest of these absolute differences is `3`.
73+
74+
## Sample Input 1
75+
76+
```text
77+
10
78+
-59 -36 -13 1 -53 -92 -2 -96 -54 75
79+
```
80+
81+
## Sample Output 1
82+
83+
```text
84+
1
85+
```
86+
87+
## Explanation 1
88+
89+
The smallest absolute difference is $ \lvert - 54 - -53 \rvert = 1$.
90+
91+
## Sample Input 2
92+
93+
```text
94+
5
95+
1 -3 71 68 17
96+
```
97+
98+
## Sample Output 2
99+
100+
```text
101+
3
102+
```
103+
104+
## Explanation 2
105+
106+
The minimum absolute difference is $ \lvert - 71 - 68 \rvert = 3$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/minimum-absolute-difference-in-an-array.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.greedy_algorithms;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public class MinimumAbsoluteDifferenceInAnArray
8+
{
9+
[ExcludeFromCodeCoverage]
10+
private MinimumAbsoluteDifferenceInAnArray()
11+
{
12+
}
13+
14+
/**
15+
* minimumAbsoluteDifference.
16+
*/
17+
public static int minimumAbsoluteDifference(List<int> arr)
18+
{
19+
List<int> sortedNums = [.. arr.ConvertAll(x => x).OrderBy(x => x).ToList()];
20+
21+
// Find the minimum absolute difference
22+
int result = 0;
23+
bool resultEmpty = true;
24+
25+
for (int i = 0; i < sortedNums.Count - 1; i++)
26+
{
27+
int aValue = sortedNums[i];
28+
int bValue = sortedNums[i + 1];
29+
30+
int diff = Math.Abs(aValue - bValue);
31+
32+
if (resultEmpty)
33+
{
34+
result = diff;
35+
resultEmpty = false;
36+
}
37+
else
38+
{
39+
result = Math.Min(result, diff);
40+
}
41+
}
42+
43+
return result;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"input": [3, -7, 0],
5+
"expected": 3
6+
},
7+
{
8+
"title": "Sample Test case 1",
9+
"input": [-59, -36, -13, 1, -53, -92, -2, -96, -54, 75],
10+
"expected": 1
11+
},
12+
{
13+
"title": "Sample Test case 2",
14+
"input": [1, -3, 71, 68, 17],
15+
"expected": 3
16+
}
17+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.greedy_algorithms;
2+
using algorithm_exercises_csharp_test.lib;
3+
using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.greedy_algorithms;
4+
5+
[TestClass]
6+
public class MinimumAbsoluteDifferenceInAnArrayTest
7+
{
8+
public class MinimumAbsoluteDifferenceInAnArrayTestCase
9+
{
10+
public string title = "";
11+
public List<int> input = [];
12+
public int expected;
13+
}
14+
15+
private List<MinimumAbsoluteDifferenceInAnArrayTestCase> testCases { get; set; } = default!;
16+
17+
[TestInitialize]
18+
public void testInitialize()
19+
{
20+
testCases = JsonLoader.resourceLoad<List<MinimumAbsoluteDifferenceInAnArrayTestCase>>(
21+
"hackerrank/interview_preparation_kit/greedy_algorithms/minimum_absolute_difference_in_an_array.testcases.json"
22+
) ?? [];
23+
}
24+
25+
[TestMethod]
26+
public void testMinimumAbsoluteDifferenceInAnArray()
27+
{
28+
int result;
29+
30+
foreach (MinimumAbsoluteDifferenceInAnArrayTestCase test in testCases)
31+
{
32+
result = MinimumAbsoluteDifferenceInAnArray.minimumAbsoluteDifference(test.input);
33+
Assert.AreEqual(
34+
test.expected,
35+
result,
36+
String.Format(
37+
"minimumAbsoluteDifference({0}) => must be: {1}",
38+
test.input.ToString(),
39+
test.expected
40+
)
41+
);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)