Connect with us

HackerRank Solutions

Minimum Absolute Difference in an Array Hacker Rank Solution Best & Easiest

Published

on

Minimum Absolute Difference in an Array Problem Solution

In this post, we will solve the Minimum Absolute Difference in an Array Jumps HackerRank Solution. This problem (Minimum Absolute Difference in an Array) is a part of the HackerRank Problem Solving series.

Consider an array of integers, arr =[arr[0], arr[1], … , arr[n-1]] . We define the absolute difference between two elements, a[i] and a[j] (where i != j), to be the absolute value of a[i] – a[j] .

Given an array of integers, find and print the minimum absolute difference between any two elements in the array. For example, given the array arr = [-2,2,4]  we can create 3 pairs of numbers: [-2,2], [-2,4]  and [2,4]. The absolute differences for these pairs are |(-2) – 2| = 4 , |(-2) – 4| = 6  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):

  • n: an integer that represents the length of arr
  • arr: an array of integers

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 <= 100000
  • -1000000000 <= arr[i] <= 1000000000

Output Format

Print the minimum absolute difference between any two elements in the array.

Sample Input 0

3
3 -7 0

Sample Output 0

3

Explanation 0

With n = 3 integers in our array, we have three possible pairs: (3, -7), (3,0), and (-7,0). The absolute values of the differences between these pairs are as follows:

  • | 3 – -7| = 10
  • |3 – 0| = 3
  • |-7 – 0| = 7

Notice that if we were to switch the order of the numbers in these pairs, the resulting absolute values would still be the same. The smallest of these possible 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.

Minimum Absolute Difference in an Array Hacker Rank Solution

Problem solution in Java programming:

  public static int minimumAbsoluteDifference(List<Integer> arr) {
    // Write your code here
        Collections.sort(arr); 
                
        int min = Integer.MAX_VALUE;
        
        for(int i=0; i<arr.size()-1; i++){

            if(arr.get(i+1)-arr.get(i) < min){
                min = arr.get(i+1)-arr.get(i);
            }
        }
        
        return min;
    }

Problem solution in JavaScript programming:

function minimumAbsoluteDifference(arr) {
    let sortedArray = arr.sort(function(a,b){return a-b})
    let min = +Infinity;

for (let i=0;i<sortedArray.length-1;i++){
    let diff = sortedArray[i+1] - sortedArray[i];
    
    min = Math.min(diff,min)
}

return min
}

Problem solution in Python programming:

def minimumAbsoluteDifference(arr):
    # Write your code here
    s = set()
    arr.sort()
    for i in range(len(arr)-1):
        diff = arr[i+1]-arr[i]
        if diff == 0:
            return 0
        elif (arr[i+1]-arr[i]) not in s:
            s.add(arr[i+1]-arr[i])
    return min(s)

Problem solution in C++ programming:

int minimumAbsoluteDifference(vector<int> arr) {
    int mindiff = INT_MAX;
    
    sort(arr.begin() , arr.end());
    
    for(int i = 0;i<arr.size() -1;++i)
    {
        if(arr[i+1] - arr[i] < mindiff)
            mindiff = arr[i+1] - arr[i];
    }
    
    return mindiff;
}
Continue Reading
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Trending