In this HackerRank Minimum Absolute Difference interview preparation kit problem you have Given an array of integers, find the minimum absolute difference between any two elements in the array.


HackerRank Minimum Absolute Difference in an Array solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the minimumAbsoluteDifference function below.
def minimumAbsoluteDifference(arr):
    diffs = []
    arr.sort()
    for i in range(len(arr)-1):
        diffs.append(abs(arr[i]-arr[i+1]))
    return min(diffs)

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    arr = list(map(int, input().rstrip().split()))

    result = minimumAbsoluteDifference(arr)

    fptr.write(str(result) + '\n')

    fptr.close()



Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for(int a_i=0; a_i < n; a_i++){
            a[a_i] = in.nextInt();
        }
        Arrays.sort(a);
        int min = 2000000000;
        for (int i = 1; i < n; i++) {
            min = Math.min(Math.abs(a[i]-a[i-1]), min);
        }
        System.out.println(min);
    }
}


Problem solution in C++ programming.

#include <bits/stdc++.h>

using namespace std;

const int maxN = 1e5+10;
int N,A[maxN];

int main()
{
    cin >> N;
    for (int i=1; i <= N; i++) cin >> A[i];
    sort(A+1,A+N+1);
    int ans = abs(A[2]-A[1]);
    for (int i=2; i <= N; i++) ans = min(ans,abs(A[i]-A[i-1]));
    cout << ans;
}


Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int compare_int( const void* a, const void* b )
{
    if( *(int*)a == *(int*)b ) return 0;
    return *(int*)a < *(int*)b ? -1 : 1;
}

int main(){
    int n; 
    scanf("%d",&n);
    int *a = malloc(sizeof(int) * n);
    for(int a_i = 0; a_i < n; a_i++){
       scanf("%d",&a[a_i]);
    }
    // your code goes here
    
    qsort( a, n, sizeof(int), compare_int );
    int minDiff = abs(a[1] - a[0]);
    for(int i=1; i<n-1; ++i){
        if( abs(a[i] - a[i+1]) < minDiff ) minDiff = abs(a[i] - a[i+1]);
    }
    printf("%d", minDiff);
    return 0;
}


Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var n = parseInt(readLine());
    a = readLine().split(' ');
    a = a.map(Number);
    a.sort();

    const length = a.length;
    let min = Infinity; // Initial minimum absolute value.
    for(let i = 1; i < length; i += 1) {
      const prev = a[i - 1];
      const current = a[i];
      const diff = Math.abs(prev - current);
      min = Math.min(min, diff);
    }

    console.log(min);
}