Skip to content

Commit 4ea622e

Browse files
authored
Merge pull request #314 from sir-gon/feature/minimum-absolute-difference-in-an-array
[Hacker Rank] Interview Preparation Kit: Greedy Algorithms: Minimum A…
2 parents 1b7dd57 + 3893df0 commit 4ea622e

File tree

4 files changed

+216
-0
lines changed

4 files changed

+216
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package ae.hackerrank.interview_preparation_kit.greedy_algorithms;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* MinimumAbsoluteDifferenceInAnArray.
9+
*
10+
* @link Problem definition
11+
* [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/minimum-absolute-difference-in-an-array.md]]
12+
*/
13+
public class MinimumAbsoluteDifferenceInAnArray {
14+
15+
private MinimumAbsoluteDifferenceInAnArray() {
16+
}
17+
18+
/**
19+
* minimumAbsoluteDifference.
20+
*/
21+
public static int minimumAbsoluteDifference(List<Integer> arr) {
22+
List<Integer> sortedNums = arr.stream().collect(Collectors.toList());
23+
Collections.sort(sortedNums);
24+
25+
// Find the minimum absolute difference
26+
int result = 0;
27+
boolean resultEmpty = true;
28+
29+
for (int i = 0; i < sortedNums.size() - 1; i++) {
30+
int aValue = sortedNums.get(i);
31+
int bValue = sortedNums.get(i + 1);
32+
33+
int diff = Math.abs(aValue - bValue);
34+
35+
if (resultEmpty) {
36+
result = diff;
37+
resultEmpty = false;
38+
} else {
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,48 @@
1+
package ae.hackerrank.interview_preparation_kit.greedy_algorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
import util.JsonLoader;
12+
13+
/**
14+
* MinimumAbsoluteDifferenceInAnArrayTest.
15+
*/
16+
@TestInstance(Lifecycle.PER_CLASS)
17+
class MinimumAbsoluteDifferenceInAnArrayTest {
18+
public static class MinimumAbsoluteDifferenceInAnArrayTestCase {
19+
public String title;
20+
public List<Integer> input;
21+
public Integer expected;
22+
}
23+
24+
private List<MinimumAbsoluteDifferenceInAnArrayTestCase> testCases;
25+
26+
@BeforeAll
27+
void setup() throws IOException {
28+
String path = String.join("/",
29+
"hackerrank",
30+
"interview_preparation_kit",
31+
"greedy_algorithms",
32+
"minimum_absolute_difference_in_an_array.testcases.json");
33+
this.testCases = JsonLoader.loadJson(path, MinimumAbsoluteDifferenceInAnArrayTestCase.class);
34+
}
35+
36+
@Test
37+
void testLuckBalance() {
38+
for (MinimumAbsoluteDifferenceInAnArrayTestCase test : testCases) {
39+
Integer result = MinimumAbsoluteDifferenceInAnArray.minimumAbsoluteDifference(test.input);
40+
41+
assertEquals(test.expected, result,
42+
"%s(%s) => must be: %d".formatted(
43+
"MinimumAbsoluteDifferenceInAnArray.minimumAbsoluteDifference",
44+
test.input.toString(),
45+
test.expected));
46+
}
47+
}
48+
}
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+
]
Lines changed: 106 additions & 0 deletions
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$.

0 commit comments

Comments
 (0)