Skip to content

Commit d5c4e56

Browse files
authored
Merge pull request #344 from sir-gon/feature/minimum_absolute_difference_in_an_array
[Hacker Rank] Interview Preparation Kit: Greedy Algorithms: Minimum A…
2 parents ca82cd6 + 7983629 commit d5c4e56

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed
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$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @link Problem definition
3+
* [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/minimum-absolute-difference-in-an-array.md]]
4+
*/
5+
6+
package hackerrank
7+
8+
import "slices"
9+
10+
func minimumAbsoluteDifference(arr []int32) int32 {
11+
// Sort the array
12+
arrCopy := make([]int32, len(arr))
13+
14+
copy(arrCopy, arr)
15+
slices.Sort(arrCopy)
16+
17+
// Find the minimum absolute difference
18+
result := int32(0)
19+
for i := range len(arrCopy) - 1 {
20+
diff := arrCopy[i+1] - arrCopy[i]
21+
if diff < result || result == 0 {
22+
result = diff
23+
}
24+
}
25+
return result
26+
}
27+
28+
func MinimumAbsoluteDifferenceInAnArray(arr []int32) int32 {
29+
return minimumAbsoluteDifference(arr)
30+
}
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+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gon.cl/algorithms/utils"
10+
)
11+
12+
type MinimumAbsoluteDifferenceInAnArrayTestCase struct {
13+
Input []int32 `json:"input"`
14+
Expected int32 `json:"expected"`
15+
}
16+
17+
var MinimumAbsoluteDifferenceInAnArrayTestCases []MinimumAbsoluteDifferenceInAnArrayTestCase
18+
19+
// You can use testing.T, if you want to test the code without benchmarking
20+
func MinimumAbsoluteDifferenceInAnArraySetupSuite(t testing.TB) {
21+
wd, _ := os.Getwd()
22+
filepath := wd + "/minimum_absolute_difference_in_an_array.testcases.json"
23+
t.Log("Setup test cases from JSON: ", filepath)
24+
25+
var _, err = utils.LoadJSON(filepath, &MinimumAbsoluteDifferenceInAnArrayTestCases)
26+
if err != nil {
27+
t.Log(err)
28+
}
29+
}
30+
31+
func TestMinimumAbsoluteDifferenceInAnArray(t *testing.T) {
32+
33+
MinimumAbsoluteDifferenceInAnArraySetupSuite(t)
34+
35+
for _, tt := range MinimumAbsoluteDifferenceInAnArrayTestCases {
36+
testname := fmt.Sprintf("MinimumAbsoluteDifferenceInAnArray(%v) => %v \n", tt.Input, tt.Expected)
37+
t.Run(testname, func(t *testing.T) {
38+
ans := MinimumAbsoluteDifferenceInAnArray(tt.Input)
39+
assert.Equal(t, tt.Expected, ans)
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)