# Find minimum difference between any two elements (pair) in given array

• Difficulty Level : Basic
• Last Updated : 15 Sep, 2022

Given an unsorted array, find the minimum difference between any pair in the given array.

Examples :

Input: {1, 5, 3, 19, 18, 25}
Output: 1
Explanation: Minimum difference is between 18 and 19

Input: {30, 5, 20, 9}
Output: 4
Explanation: Minimum difference is between 5 and 9

Input: {1, 19, -4, 31, 38, 25, 100}
Output: 5
Explanation: Minimum difference is between 1 and -4

Recommended Practice

Naive Approach: To solve the problem follow the below idea:

A simple solution is to use two loops two generate every pair of elements and compare them to get the minimum difference

Below is the implementation of the above approach:

## C++

 `// C++ implementation of simple method to find``// minimum difference between any pair``#include ``using` `namespace` `std;` `// Returns minimum difference between any pair``int` `findMinDiff(``int` `arr[], ``int` `n)``{``    ``// Initialize difference as infinite``    ``int` `diff = INT_MAX;` `    ``// Find the min diff by comparing difference``    ``// of all possible pairs in given array``    ``for` `(``int` `i = 0; i < n - 1; i++)``        ``for` `(``int` `j = i + 1; j < n; j++)``            ``if` `(``abs``(arr[i] - arr[j]) < diff)``                ``diff = ``abs``(arr[i] - arr[j]);` `    ``// Return min diff``    ``return` `diff;``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 5, 3, 19, 18, 25 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Function call``    ``cout << ``"Minimum difference is "` `<< findMinDiff(arr, n);``    ``return` `0;``}`

## Java

 `// Java implementation of simple method to find``// minimum difference between any pair` `class` `GFG {``    ``// Returns minimum difference between any pair``    ``static` `int` `findMinDiff(``int``[] arr, ``int` `n)``    ``{``        ``// Initialize difference as infinite``        ``int` `diff = Integer.MAX_VALUE;` `        ``// Find the min diff by comparing difference``        ``// of all possible pairs in given array``        ``for` `(``int` `i = ``0``; i < n - ``1``; i++)``            ``for` `(``int` `j = i + ``1``; j < n; j++)``                ``if` `(Math.abs((arr[i] - arr[j])) < diff)``                    ``diff = Math.abs((arr[i] - arr[j]));` `        ``// Return min diff``        ``return` `diff;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = ``new` `int``[] { ``1``, ``5``, ``3``, ``19``, ``18``, ``25` `};` `        ``// Function call``        ``System.out.println(``"Minimum difference is "``                           ``+ findMinDiff(arr, arr.length));``    ``}``}`

## Python3

 `# Python implementation of simple method to find``# minimum difference between any pair` `# Returns minimum difference between any pair`  `def` `findMinDiff(arr, n):``    ``# Initialize difference as infinite``    ``diff ``=` `10``*``*``20` `    ``# Find the min diff by comparing difference``    ``# of all possible pairs in given array``    ``for` `i ``in` `range``(n``-``1``):``        ``for` `j ``in` `range``(i``+``1``, n):``            ``if` `abs``(arr[i]``-``arr[j]) < diff:``                ``diff ``=` `abs``(arr[i] ``-` `arr[j])` `    ``# Return min diff``    ``return` `diff`  `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ``arr ``=` `[``1``, ``5``, ``3``, ``19``, ``18``, ``25``]``    ``n ``=` `len``(arr)` `    ``# Function call``    ``print``(``"Minimum difference is "` `+` `str``(findMinDiff(arr, n)))` `# This code is contributed by Pratik Chhajer`

## C#

 `// C# implementation of simple method to find``// minimum difference between any pair``using` `System;` `class` `GFG {` `    ``// Returns minimum difference between any pair``    ``static` `int` `findMinDiff(``int``[] arr, ``int` `n)``    ``{` `        ``// Initialize difference as infinite``        ``int` `diff = ``int``.MaxValue;` `        ``// Find the min diff by comparing difference``        ``// of all possible pairs in given array``        ``for` `(``int` `i = 0; i < n - 1; i++)``            ``for` `(``int` `j = i + 1; j < n; j++)``                ``if` `(Math.Abs((arr[i] - arr[j])) < diff)``                    ``diff = Math.Abs((arr[i] - arr[j]));` `        ``// Return min diff``        ``return` `diff;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``int``[] arr = ``new` `int``[] { 1, 5, 3, 19, 18, 25 };` `        ``// Function call``        ``Console.Write(``"Minimum difference is "``                      ``+ findMinDiff(arr, arr.Length));``    ``}``}` `// This code is contributed by nitin mittal.`

## PHP

 ``

## Javascript

 ``

Output

`Minimum difference is 1`

Time Complexity: O(N2).
Auxiliary Space: O(1)

## Find the minimum difference between any two elements using sorting:

The idea is to use sorting and compare every adjacent pair of the array

Follow the given steps to solve the problem:

• Sort array in ascending order
• Initialize difference as infinite
• Compare all adjacent pairs in a sorted array and keep track of the minimum difference

Below is the implementation of the above approach:

## C++

 `// C++ program to find minimum difference between``// any pair in an unsorted array``#include ``using` `namespace` `std;` `// Returns minimum difference between any pair``int` `findMinDiff(``int` `arr[], ``int` `n)``{``    ``// Sort array in non-decreasing order``    ``sort(arr, arr + n);` `    ``// Initialize difference as infinite``    ``int` `diff = INT_MAX;` `    ``// Find the min diff by comparing adjacent``    ``// pairs in sorted array``    ``for` `(``int` `i = 0; i < n - 1; i++)``        ``if` `(arr[i + 1] - arr[i] < diff)``            ``diff = arr[i + 1] - arr[i];` `    ``// Return min diff``    ``return` `diff;``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 5, 3, 19, 18, 25 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Function call``    ``cout << ``"Minimum difference is "` `<< findMinDiff(arr, n);``    ``return` `0;``}`

## Java

 `// Java program to find minimum difference between``// any pair in an unsorted array` `import` `java.util.Arrays;` `class` `GFG {``    ``// Returns minimum difference between any pair``    ``static` `int` `findMinDiff(``int``[] arr, ``int` `n)``    ``{``        ``// Sort array in non-decreasing order``        ``Arrays.sort(arr);` `        ``// Initialize difference as infinite``        ``int` `diff = Integer.MAX_VALUE;` `        ``// Find the min diff by comparing adjacent``        ``// pairs in sorted array``        ``for` `(``int` `i = ``0``; i < n - ``1``; i++)``            ``if` `(arr[i + ``1``] - arr[i] < diff)``                ``diff = arr[i + ``1``] - arr[i];` `        ``// Return min diff``        ``return` `diff;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = ``new` `int``[] { ``1``, ``5``, ``3``, ``19``, ``18``, ``25` `};` `        ``// Function call``        ``System.out.println(``"Minimum difference is "``                           ``+ findMinDiff(arr, arr.length));``    ``}``}`

## Python3

 `# Python3 program to find minimum difference between``# any pair in an unsorted array` `# Returns minimum difference between any pair`  `def` `findMinDiff(arr, n):` `    ``# Sort array in non-decreasing order``    ``arr ``=` `sorted``(arr)` `    ``# Initialize difference as infinite``    ``diff ``=` `10``*``*``20` `    ``# Find the min diff by comparing adjacent``    ``# pairs in sorted array``    ``for` `i ``in` `range``(n``-``1``):``        ``if` `arr[i``+``1``] ``-` `arr[i] < diff:``            ``diff ``=` `arr[i``+``1``] ``-` `arr[i]` `    ``# Return min diff``    ``return` `diff`  `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ``arr ``=` `[``1``, ``5``, ``3``, ``19``, ``18``, ``25``]``    ``n ``=` `len``(arr)` `    ``# Function call``    ``print``(``"Minimum difference is "` `+` `str``(findMinDiff(arr, n)))` `# This code is contributed by Pratik Chhajer`

## C#

 `// C# program to find minimum``// difference between any pair``// in an unsorted array``using` `System;` `class` `GFG {``    ``// Returns minimum difference``    ``// between any pair``    ``static` `int` `findMinDiff(``int``[] arr, ``int` `n)``    ``{``        ``// Sort array in``        ``// non-decreasing order``        ``Array.Sort(arr);` `        ``// Initialize difference``        ``// as infinite``        ``int` `diff = ``int``.MaxValue;` `        ``// Find the min diff by``        ``// comparing adjacent pairs``        ``// in sorted array``        ``for` `(``int` `i = 0; i < n - 1; i++)``            ``if` `(arr[i + 1] - arr[i] < diff)``                ``diff = arr[i + 1] - arr[i];` `        ``// Return min diff``        ``return` `diff;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int``[] arr = ``new` `int``[] { 1, 5, 3, 19, 18, 25 };` `        ``// Function call``        ``Console.WriteLine(``"Minimum difference is "``                          ``+ findMinDiff(arr, arr.Length));``    ``}``}` `// This code is contributed by anuj_67.`

## PHP

 ``

## Javascript

 ``

Output

`Minimum difference is 1`

Time Complexity: O(N log N)
Auxiliary Space: O(1)