Skip to content

Commit 375a4f6

Browse files
authored
Merge pull request #127 from sir-gon/feature/minimum-absolute-difference-in-an-array
[Hacker Rank]: Minimum Absolute Difference in an Array solved ✓
2 parents 6677801 + 0abe035 commit 375a4f6

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import logger from '../../logger';
2+
3+
import { minimumAbsoluteDifference } from './minimumAbsoluteDifference';
4+
5+
describe('Minimum Absolute Difference in an Array', () => {
6+
it('Minimum Absolute Difference in an Array border case', () => {
7+
expect.assertions(1);
8+
9+
expect(() => {
10+
minimumAbsoluteDifference([]);
11+
}).toThrow('Empty input');
12+
});
13+
14+
it('Minimum Absolute Difference in an Array Test case 0', () => {
15+
expect.assertions(1);
16+
17+
const input = [3, -7, 0];
18+
const solutionFound = 3;
19+
20+
const calculated = minimumAbsoluteDifference(input);
21+
22+
logger.info(
23+
`Minimum Absolute Difference in an Array Test case 0: ${calculated}`
24+
);
25+
26+
expect(calculated).toStrictEqual(solutionFound);
27+
});
28+
29+
it('Minimum Absolute Difference in an Array Test case 1', () => {
30+
expect.assertions(1);
31+
32+
const input = [-59, -36, -13, 1, -53, -92, -2, -96, -54, 75];
33+
const solutionFound = 1;
34+
35+
const calculated = minimumAbsoluteDifference(input);
36+
37+
logger.info(
38+
`Minimum Absolute Difference in an Array Test case 1: ${calculated}`
39+
);
40+
41+
expect(calculated).toStrictEqual(solutionFound);
42+
});
43+
});
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Minimum Absolute Difference in an Array
3+
*
4+
* https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array
5+
*
6+
* The absolute difference is the positive difference between two values
7+
* a and b, is written |a - b| or |b - a| and they are equal. If a = 3 and
8+
* b = 2, |3 - 2| = |2 - 3| = 1. Given an array of integers, find the minimum
9+
* absolute difference between any two elements in the array.
10+
*
11+
* # Example.
12+
* arr = [-2, 2, 4]
13+
*
14+
* There are 3 pairs of numbers: [-2, 2], [-2, 4] and [2, 4]. The absolute
15+
* differences for these pairs are |(-2)| - 2| = 4, |(-2) - 4| and |2 - 4| = 2.
16+
* The minimum absolute difference is 2.
17+
*
18+
* # Function Description
19+
*
20+
* Complete the minimumAbsoluteDifference function in the editor below.
21+
* It should return an integer that represents the minimum absolute difference
22+
* between any pair of elements.
23+
*
24+
* minimumAbsoluteDifference has the following parameter(s):
25+
*
26+
* * int arr[n]: an array of integers
27+
*
28+
* # Returns
29+
*
30+
* * int: the minimum absolute difference found
31+
* Input Format
32+
*
33+
* The first line contains a single integer n, the size of arr.
34+
* The second line contains n space-separated integers, arr[i].
35+
*
36+
* # Constraints
37+
* 2 <= n < 10<sup>5</sup>
38+
* -10<sup>9</sup> <= arr[i] <= 10<sup>9</sup>
39+
*
40+
* Sample Input 0
41+
* ```
42+
* 3
43+
* 3 -7 0
44+
* ```
45+
*
46+
* # Sample Output 0
47+
* ```
48+
* 3
49+
* ```
50+
*
51+
* # Explanation 0
52+
*
53+
* The first line of input is the number of array elements. The array,
54+
* arr = [3, -7, 0] There are three pairs to test: (3, -7), (3, 0), and
55+
* (-7, 0). The absolute differences are:
56+
*
57+
* |3 - -7| => 10
58+
* |3 - 0| => 3
59+
* |-7 0| => 7
60+
*
61+
* Remember that the order of values in the subtraction does not influence
62+
* the result. The smallest of these absolute differences is 3.
63+
*
64+
* # Sample Input 1
65+
* ```
66+
* 10
67+
* -59 -36 -13 1 -53 -92 -2 -96 -54 75
68+
* ```
69+
*
70+
* # Sample Output 1
71+
* ```
72+
* 1
73+
* ```
74+
*
75+
* # Explanation 1
76+
*
77+
* The smallest absolute difference is |-54 - -53| = 1.
78+
*
79+
* # Sample Input 2
80+
* ```
81+
* 5
82+
* 1 -3 71 68 17
83+
* ```
84+
*
85+
* # Sample Output 2
86+
* ```
87+
* 3
88+
* ```
89+
*
90+
* # Explanation 2
91+
*
92+
* The minimum absolute difference is |71 - 68| = 3.
93+
*/
94+
95+
import { logger as console } from '../../logger';
96+
97+
export function minimumAbsoluteDifference(arr: number[]): number {
98+
if (arr.length == 0) {
99+
throw new Error('Empty input');
100+
}
101+
102+
const sortedNums = arr.splice(0).sort();
103+
console.log(`sortedNums: ${sortedNums}`);
104+
105+
let result: number = Math.abs(sortedNums[0] - sortedNums[1]);
106+
107+
for (let i = 0; i < sortedNums.length - 1; i++) {
108+
const a = sortedNums[i];
109+
const b = sortedNums[i + 1];
110+
111+
const diff = Math.abs(a - b);
112+
113+
console.debug(
114+
`(i: ${i}, i+1: ${i + 1}) => |a - b| = |${a} - ${b}| = ${diff}`
115+
);
116+
117+
result = Math.min(result, diff);
118+
}
119+
120+
return result;
121+
}
122+
123+
export default { minimumAbsoluteDifference };

0 commit comments

Comments
 (0)