Skip to content

Commit

Permalink
[Hacker Rank]: Minimum Absolute Difference doc moved as Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
sir-gon committed Jul 26, 2023
1 parent facee84 commit fa351d2
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 94 deletions.
101 changes: 101 additions & 0 deletions src/hackerrank/implementation/minimumAbsoluteDifference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

# [Minimum Absolute Difference in an Array](https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array)

Difficulty: #easy
Category: #implementation

The absolute difference is the positive difference between two values
$ a $ and $ b $, is written $ |a - b| $ or $ |b - a| $ and they are equal. If $ a = 3 $ and
$ b = 2 $, $ |3 - 2| = |2 - 3| = 1 $. Given an array of integers, find the minimum
absolute difference between any two elemednts in the array.

## Example

$ arr = [-2, 2, 4] $

There are 3 pairs of numbers: $ [-2, 2], [-2, 4] $ and $ [2, 4] $. The absolute
differences for these pairs are $ |(-2)| - 2| = 4 $, $ |(-2) - 4| $ and $ |2 - 4| = 2 $.
The minimum absolute difference is $ 2 $.

## Function Description

Complete the minimumAbsoluteDifference function in the editor below.
It should return an integer that represents the minimum absolute difference
between any pair of elements.

minimumAbsoluteDifference has the following parameter(s):

- int arr[n]: an array of integers

## Returns

- int: the minimum absolute difference found
Input Format

The first line contains a single integer n, the size of arr.
The second line contains n space-separated integers, arr[i].

## Constraints

$ 2 \leq n < 10^5 $ \
$ -10^9 \leq arr[i] \leq 10^9 $

Sample Input 0

```text
3
3 -7 0
```

## Sample Output 0

```text
3
```

## Explanation 0

The first line of input is the number of array elements. The array,
$ arr = [3, -7, 0] $ There are three pairs to test: $ (3, -7) $, $ (3, 0) $, and
$ (-7, 0) $. The absolute differences are:

$ |3 - -7| => 10 $ \
$ |3 - 0| => 3 $ \
$ |-7 0| => 7 $

Remember that the order of values in the subtraction does not influence
the result. The smallest of these absolute differences is 3.

## Sample Input 1

```text
10
-59 -36 -13 1 -53 -92 -2 -96 -54 75
```

## Sample Output 1

```text
1
```

## Explanation 1

The smallest absolute difference is $ |-54 - -53| = 1 $.

## Sample Input 2

```text
5
1 -3 71 68 17
```

## Sample Output 2

```text
3
```

## Explanation 2

The minimum absolute difference is $ |71 - 68| = 3 $.
94 changes: 0 additions & 94 deletions src/hackerrank/implementation/minimumAbsoluteDifference.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,3 @@
/**
* Minimum Absolute Difference in an Array
*
* https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array
*
* The absolute difference is the positive difference between two values
* a and b, is written |a - b| or |b - a| and they are equal. If a = 3 and
* b = 2, |3 - 2| = |2 - 3| = 1. Given an array of integers, find the minimum
* absolute difference between any two elements in the array.
*
* # Example.
* arr = [-2, 2, 4]
*
* There are 3 pairs of numbers: [-2, 2], [-2, 4] and [2, 4]. The absolute
* differences for these pairs are |(-2)| - 2| = 4, |(-2) - 4| and |2 - 4| = 2.
* The minimum absolute difference is 2.
*
* # Function Description
*
* Complete the minimumAbsoluteDifference function in the editor below.
* It should return an integer that represents the minimum absolute difference
* between any pair of elements.
*
* minimumAbsoluteDifference has the following parameter(s):
*
* * int arr[n]: an array of integers
*
* # Returns
*
* * int: the minimum absolute difference found
* Input Format
*
* The first line contains a single integer n, the size of arr.
* The second line contains n space-separated integers, arr[i].
*
* # Constraints
* 2 <= n < 10<sup>5</sup>
* -10<sup>9</sup> <= arr[i] <= 10<sup>9</sup>
*
* Sample Input 0
* ```
* 3
* 3 -7 0
* ```
*
* # Sample Output 0
* ```
* 3
* ```
*
* # Explanation 0
*
* The first line of input is the number of array elements. The array,
* arr = [3, -7, 0] There are three pairs to test: (3, -7), (3, 0), and
* (-7, 0). The absolute differences are:
*
* |3 - -7| => 10
* |3 - 0| => 3
* |-7 0| => 7
*
* Remember that the order of values in the subtraction does not influence
* the result. The smallest of these absolute differences is 3.
*
* # Sample Input 1
* ```
* 10
* -59 -36 -13 1 -53 -92 -2 -96 -54 75
* ```
*
* # Sample Output 1
* ```
* 1
* ```
*
* # Explanation 1
*
* The smallest absolute difference is |-54 - -53| = 1.
*
* # Sample Input 2
* ```
* 5
* 1 -3 71 68 17
* ```
*
* # Sample Output 2
* ```
* 3
* ```
*
* # Explanation 2
*
* The minimum absolute difference is |71 - 68| = 3.
*/

import { logger as console } from '../../logger';

export function minimumAbsoluteDifference(arr: number[]): number {
Expand Down

0 comments on commit fa351d2

Please sign in to comment.