Minimum Absolute Difference in an Array


Problem Statement :


The absolute difference is the positive difference between two values a and b, is written | a - b | or | b - a |  and they are equal. If a = 3 and  b = 20,  | 3 - 2 | = | 2 - 3 | = 1 . 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


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   <=   10^5
- 10^9   <=   arr[ i ]   <=  10^9


Sample Input 0

3
3 -7 0

Sample Output 0

3



Solution :



title-img


                            Solution in C :

In  C  :






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

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int main(){
    int n,min,i; 
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    qsort(a, n, sizeof(int), cmpfunc);
    min=a[1]-a[0];
    for(i=0;i<n-1;i++){
        if(min>(a[i+1]-a[i])){
            min=(a[i+1]-a[i]);
        }
    }
    printf("%d",min);
    return 0;
}
                        


                        Solution in C++ :

In C++  :







#include<bits/stdc++.h>

using namespace std;

int main(){
    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    sort(a, a + n);
    int ans = 2000000001;
    for(int i = 1; i < n; i++){
        if(a[i] - a[i - 1] < ans){
            ans = a[i] - a[i - 1];
        }
    }
    cout << ans;
}
                    


                        Solution in Java :

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 minimum=999999999;
        for(int i = 1; i  <  (a.length - 1); i++){
         int temp = Math.abs(a[i+1] - a[i]);
         if (temp  <  minimum)
            minimum = temp;
      }
        // your code goes here
        System.out.println(minimum);
    }
}
                    


                        Solution in Python : 
                            
In  Python3 :






#!/bin/python3

import sys


n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
# your code goes here
a.sort()
m=a[-1]-a[0]
for i in range(1,n):
    if a[i]-a[i-1]<m:
        m=a[i]-a[i-1]
print(m)
                    


View More Similar Problems

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →