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 19Input: [30, 5, 20, 9]
Output: 4
Explanation: Minimum difference is between 5 and 9Input: [1, 19, -4, 31, 38, 25, 100]
Output: 5
Explanation: Minimum difference is between 1 and -4
Table of Content
[Naive Approach] Using Nested Loops – O(n²) Time and O(1) Space
The idea is to compare every possible pair of elements in the array and calculate their absolute differenceand keep track of the minimum diff.
- Initialize the minimum difference as infinity
- Use two nested loops to generate all possible pairs
- Compute absolute difference for each pair and update minimum value
- Return the smallest difference obtained after all comparisons
#include <iostream>
#include <vector>
#include <climits>
#include <cmath>
using namespace std;
// Returns minimum difference between any pair
int minDiff(vector<int>& arr)
{
int n = arr.size();
// 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()
{
vector<int> arr = {1, 5, 3, 19, 18, 25};
// Function call
cout << minDiff(arr);
return 0;
}
import java.util.*;
class GFG {
// Returns minimum difference between any pair
static int minDiff(int[] arr)
{
int n = arr.length;
// 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 = {1, 5, 3, 19, 18, 25};
// Function call
System.out.println(minDiff(arr));
}
}
# Returns minimum difference between any pair
def minDiff(arr):
n = len(arr)
# Initialize difference as infinite
diff = float('inf')
# 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
arr = [1, 5, 3, 19, 18, 25]
# Function call
print(minDiff(arr))
using System;
class GFG {
// Returns minimum difference between any pair
static int minDiff(int[] arr)
{
int n = arr.Length;
// 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
static void Main()
{
int[] arr = {1, 5, 3, 19, 18, 25};
// Function call
Console.WriteLine(minDiff(arr));
}
}
// Returns minimum difference between any pair
function minDiff(arr)
{
let n = arr.length;
// Initialize difference as infinite
let diff = Number.MAX_SAFE_INTEGER;
// Find the min diff by comparing difference
// of all possible pairs in given array
for (let i = 0; i < n - 1; i++)
{
for (let 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
let arr = [1, 5, 3, 19, 18, 25];
// Function call
console.log(minDiff(arr));
Output
Minimum difference is 1
[Efficient Approach] Using Sorting – O(n log n) Time and O(1) Space
The idea is to first sort the array so that elements with the smallest difference become adjacent to each other. After sorting, instead of checking all possible pairs, we only compare consecutive elements. This works because the minimum difference in a sorted array will always occur between neighboring elements, making the approach much more efficient.
- Sort the array in non-decreasing order
- Initialize the minimum difference as infinity
- Traverse the sorted array and compare adjacent elements
- Update and return the smallest difference found
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
// Returns minimum difference between any pair
int minDiff(vector<int>& arr)
{
int n = arr.size();
// Sort array in non-decreasing order
sort(arr.begin(), arr.end());
// 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()
{
vector<int> arr = {1, 5, 3, 19, 18, 25};
// Function call
cout << "Minimum difference is " << minDiff(arr);
return 0;
}
import java.util.*;
class GFG {
// Returns minimum difference between any pair
static int minDiff(int[] arr)
{
int n = arr.length;
// 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 = {1, 5, 3, 19, 18, 25};
// Function call
System.out.println("Minimum difference is " + minDiff(arr));
}
}
# Returns minimum difference between any pair
def minDiff(arr):
n = len(arr)
# Sort array in non-decreasing order
arr.sort()
# Initialize difference as infinite
diff = float('inf')
# 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
arr = [1, 5, 3, 19, 18, 25]
# Function call
print("Minimum difference is", minDiff(arr))
using System;
class GFG {
// Returns minimum difference between any pair
static int minDiff(int[] arr)
{
int n = arr.Length;
// 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
static void Main()
{
int[] arr = {1, 5, 3, 19, 18, 25};
// Function call
Console.WriteLine("Minimum difference is " + minDiff(arr));
}
}
// Returns minimum difference between any pair
function minDiff(arr)
{
let n = arr.length;
// Sort array in non-decreasing order
arr.sort((a, b) => a - b);
// Initialize difference as infinite
let diff = Number.MAX_SAFE_INTEGER;
// Find the min diff by comparing adjacent
// pairs in sorted array
for (let 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
let arr = [1, 5, 3, 19, 18, 25];
// Function call
console.log("Minimum difference is " + minDiff(arr));
Output
Minimum difference is 1