|
| 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