# Find minimum difference between any two elements | Set 2

Given an unsorted array **arr[]** of size **n**, the task is to find the minimum difference between any pair in the given array.

Input:arr[] = {1, 2, 3, 4}

Output:1

The possible absolute differences are:

{1, 2, 3, 1, 2, 1}

Input:arr[] = {10, 2, 5, 4}

Output:1

**Approach:**

- Traverse through the array and create a hash array to store the frequency of the array elements.
- Now, traverse through the hash array and calculate the distance between two nearest elements.
- The frequency was calculated to check whether some element has frequency > 1 which means the absolute distance will be 0 i.e. |arr[i] – arr[i]|.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define MAX 100001 ` ` ` `// Function to return the minimum ` `// absolute difference between any ` `// two elements of the array ` `int` `getMinDiff(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// To store the frequency of each element ` ` ` `int` `freq[MAX] = { 0 }; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Update the frequency of current element ` ` ` `freq[arr[i]]++; ` ` ` ` ` `// If current element appears more than once ` ` ` `// then the minimum absolute difference ` ` ` `// will be 0 i.e. |arr[i] - arr[i]| ` ` ` `if` `(freq[arr[i]] > 1) ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `int` `mn = INT_MAX; ` ` ` ` ` `// Checking the distance between the nearest ` ` ` `// two elements in the frequency array ` ` ` `for` `(` `int` `i = 0; i < MAX; i++) { ` ` ` `if` `(freq[i] > 0) { ` ` ` `i++; ` ` ` `int` `cnt = 1; ` ` ` `while` `((freq[i] == 0) && (i != MAX - 1)) { ` ` ` `cnt++; ` ` ` `i++; ` ` ` `} ` ` ` `mn = min(cnt, mn); ` ` ` `i--; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the minimum absolute difference ` ` ` `return` `mn; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3, 4 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << getMinDiff(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `private` `static` `final` `int` `MAX = ` `100001` `; ` ` ` `// Function to return the minimum ` `// absolute difference between any ` `// two elements of the array ` `static` `int` `getMinDiff(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// To store the frequency of each element ` ` ` `int` `[] freq = ` `new` `int` `[MAX]; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `freq[i] = ` `0` `; ` ` ` `} ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Update the frequency of current element ` ` ` `freq[arr[i]]++; ` ` ` ` ` `// If current element appears more than once ` ` ` `// then the minimum absolute difference ` ` ` `// will be 0 i.e. |arr[i] - arr[i]| ` ` ` `if` `(freq[arr[i]] > ` `1` `) ` ` ` `return` `0` `; ` ` ` `} ` ` ` ` ` `int` `mn = Integer.MAX_VALUE; ` ` ` ` ` `// Checking the distance between the nearest ` ` ` `// two elements in the frequency array ` ` ` `for` `(` `int` `i = ` `0` `; i < MAX; i++) ` ` ` `{ ` ` ` `if` `(freq[i] > ` `0` `) ` ` ` `{ ` ` ` `i++; ` ` ` `int` `cnt = ` `1` `; ` ` ` `while` `((freq[i] == ` `0` `) && (i != MAX - ` `1` `)) ` ` ` `{ ` ` ` `cnt++; ` ` ` `i++; ` ` ` `} ` ` ` `mn = Math.min(cnt, mn); ` ` ` `i--; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the minimum absolute difference ` ` ` `return` `mn; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(getMinDiff(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nidhi16bcs2007 ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementation of the approach

MAX = 100001

# Function to return the minimum

# absolute difference between any

# two elements of the array

def getMinDiff(arr, n):

# To store the frequency of each element

freq = [0 for i in range(MAX)]

for i in range(n):

# Update the frequency of current element

freq[arr[i]] += 1

# If current element appears more than once

# then the minimum absolute difference

# will be 0 i.e. |arr[i] – arr[i]|

if (freq[arr[i]] > 1):

return 0

mn = 10**9

# Checking the distance between the nearest

# two elements in the frequency array

for i in range(MAX):

if (freq[i] > 0):

i += 1

cnt = 1

while ((freq[i] == 0) and (i != MAX – 1)):

cnt += 1

i += 1

mn = min(cnt, mn)

i -= 1

# Return the minimum absolute difference

return mn

# Driver code

arr = [ 1, 2, 3, 4]

n = len(arr)

print(getMinDiff(arr, n))

# This code is contributed by Mohit Kumar

**Output:**

1

## Recommended Posts:

- Find minimum difference between any two elements
- Find an index such that difference between product of elements before and after it is minimum
- Minimize the difference between minimum and maximum elements
- Choose k array elements such that difference of maximum and minimum is minimized
- Minimum absolute difference of adjacent elements in a circular array
- Find set of m-elements with difference of any two elements is divisible by k
- Find maximum difference between nearest left and right smaller elements
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Find minimum sum such that one of every three consecutive elements is taken
- Find minimum elements after considering all possible transformations
- Find the first, second and third minimum elements in an array
- Recursive Programs to find Minimum and Maximum elements of array
- Find the minimum number of operations required to make all array elements equal
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.