Skip to content

Commit 4740c99

Browse files
authored
Merge pull request #492 from sir-gon/feature/minimum_absolute_difference_in_an_array
[Hacker Rank] Interview Preparation Kit: Greedy Algorithms: Minimum A…
2 parents d32bb6e + 24fd902 commit 4740c99

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-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`
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,22 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/minimum-absolute-difference-in-an-array.md]]
3+
*/
4+
5+
export function minimumAbsoluteDifference(arr) {
6+
const sortedNums = arr.map((x) => x).sort((a, b) => b - a);
7+
8+
let result = Math.abs(sortedNums[sortedNums.length - 1] - sortedNums[0]);
9+
10+
for (let i = 0; i < sortedNums.length - 1; i++) {
11+
const aValue = sortedNums[i];
12+
const bValue = sortedNums[i + 1];
13+
14+
const diff = Math.abs(aValue - bValue);
15+
16+
result = Math.min(result, diff);
17+
}
18+
19+
return result;
20+
}
21+
22+
export default { minimumAbsoluteDifference };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import { minimumAbsoluteDifference } from './minimum_absolute_difference_in_an_array.js';
5+
6+
import TEST_CASES from './minimum_absolute_difference_in_an_array.testcases.json';
7+
8+
describe('minimum_absolute_difference_in_an_array', () => {
9+
it('minimumAbsoluteDifference test cases', () => {
10+
expect.assertions(3);
11+
12+
TEST_CASES.forEach((test) => {
13+
const answer = minimumAbsoluteDifference(test.input);
14+
15+
console.debug(
16+
`minimumAbsoluteDifference(${test.input}) solution found: ${answer}`
17+
);
18+
19+
expect(answer).toStrictEqual(test.expected);
20+
});
21+
});
22+
});
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+
]

0 commit comments

Comments
 (0)