Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Minimum Absolute Difference in an Array problem solution

YASH PAL, 31 July 20246 February 2026

In this HackerRank Minimum Absolute Difference interview preparation kit problem solution, you are given an array of integers. Find the minimum absolute difference between any two elements in the array.

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
HackerRank Minimum Absolute Difference in an Array solution

HackerRank Minimum Absolute Difference in an Array problem solution in Python.

#!/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()

Minimum Absolute Difference in an Array problem solution in Java.

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);
}

coding problems solutions Hackerrank Problems Solutions interview prepration kit HackerRank

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes