HackerRank Minimum Absolute Difference in an Array
In this post, we will solve HackerRank Minimum Absolute Difference in an Array Problem Solution.
The absolute difference is the positive difference between two values a and b, is written labor bal 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 = 6 and 12-4=2. The minimum absolute difference is 2.
Function Description
Complete the minimumAbsolute Difference function in the editor below. It should return an integer that represents the minimum absolute difference between any pair of elements. minimumAbsolute Difference 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].
Sample Input 0
3 3 -7 0
Sample Output 0
3
Minimum Absolute Difference in an Array C Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int choose_pivot(int i,int j )
{
return((i+j) /2);
}
void quicksort(int list[],int m,int n)
{
int key,i,j,k;
if( m < n)
{
k = choose_pivot(m,n);
swap(&list[m],&list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i] <= key))
i++;
while((j >= m) && (list[j] > key))
j--;
if( i < j)
swap(&list[i],&list[j]);
}
/* swap two elements */
swap(&list[m],&list[j]);
/* recursively sort the lesser list */
quicksort(list,m,j-1);
quicksort(list,j+1,n);
}
}
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
/**int min=abs(a[0]-a[1]);
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int a1=abs(a[i]-a[j]);
if(min>a1)
min=a1;
}
}*/
quicksort(a,0,n-1);
/** for(int i=0;i<n;i++)
printf("%d\n",a[i]);*/
int min=abs(a[1]-a[0]);
for(int i=1;i<n-1;i++){
int b=abs(a[i]-a[i+1]);
if(min>b)
min=b;
}
printf("%d",min);
return 0;
}
Minimum Absolute Difference in an Array C++ Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a[100001] = {0};
int n;
cin >> n;
long long minn = INT32_MAX;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a, a + n);
for (int i = 0; i < n - 1; ++i) {
minn = min(minn, a[i + 1] - a[i]);
}
cout << minn;
return 0;
}
Minimum Absolute Difference in an Array C Sharp Solution
/*
* Minimum Absolute Difference
*
* Joel Enriquez
* 2017/02/11
*
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
string[] s = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(s,Int32.Parse);
// For now, depend on the sorting framework of C#
Array.Sort(a);
// Get minimum distance
int min = 1000000000; //10^9
for (int i = 0; i < n - 1 && min > 0; i++)
{
int diff = a[i + 1] - a[i];
if (diff < min )
{
min = diff;
}
}
// Result
Console.WriteLine(min);
}
}
Minimum Absolute Difference in an Array Java Solution
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 = Integer.MAX_VALUE;
for(int i = 0; i<n-1; i++){
int diff = Math.abs(a[i] - a[i+1]);
if(diff < min) min = diff;
}
System.out.println(min);
}
}
Minimum Absolute Difference in an Array JavaScript Solution
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);
// your code goes here
a.sort(function(a,b){
return a-b;
});
//console.log(a);
var min = 10000000000;
for(i=1;i<n;++i){
if(min > Math.abs(a[i]-a[i-1])){
min = Math.abs(a[i]-a[i-1]);
}
}
console.log(min);
}
Minimum Absolute Difference in an Array Python Solution
#!/bin/python3
import collections
import math
import sys
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
# your code goes here
a.sort()
i = 0
minDif = float("inf")
while i < n - 1:
if abs(a[i] - a[i+1]) < minDif:
minDif = abs(a[i] - a[i+1])
i += 1
print(minDif)