# Find minimum difference between any two elements

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

**Examples :**

Input : {1, 5, 3, 19, 18, 25}; Output : 1 Minimum difference is between 18 and 19 Input : {30, 5, 20, 9}; Output : 4 Minimum difference is between 5 and 9 Input : {1, 19, -4, 31, 38, 25, 100}; Output : 5 Minimum difference is between 1 and -4

**Method 1 (Simple: O(n ^{2})**

A simple solution is to use two loops.

## C/C++

`// C++ implementation of simple method to find ` `// minimum difference between any pair ` `#include <bits/stdc++.h> ` `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[0]); ` ` ` `cout << ` `"Minimum difference is "` `<< findMinDiff(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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 method to test the above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = ` `new` `int` `[]{` `1` `, ` `5` `, ` `3` `, ` `19` `, ` `18` `, ` `25` `}; ` ` ` `System.out.println(` `"Minimum difference is "` `+ ` ` ` `findMinDiff(arr, arr.length)); ` ` ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# 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 ` `arr ` `=` `[` `1` `, ` `5` `, ` `3` `, ` `19` `, ` `18` `, ` `25` `] ` `n ` `=` `len` `(arr) ` `print` `(` `"Minimum difference is "` `+` `str` `(findMinDiff(arr, n))) ` ` ` `# This code is contributed by Pratik Chhajer ` |

*chevron_right*

*filter_none*

## 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 method to test the above function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr = ` `new` `int` `[]{1, 5, 3, 19, 18, 25}; ` ` ` `Console.Write(` `"Minimum difference is "` `+ ` ` ` `findMinDiff(arr, arr.Length)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of simple ` `// method to find minimum ` `// difference between any pair ` ` ` `// Returns minimum difference ` `// between any pair ` `function` `findMinDiff(` `$arr` `, ` `$n` `) ` `{ ` `// Initialize difference ` `// as infinite ` `$diff` `= PHP_INT_MAX; ` ` ` `// Find the min diff by comparing ` `// difference of all possible ` `// pairs in given array ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `- 1; ` `$i` `++) ` ` ` `for` `(` `$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 ` `$arr` `= ` `array` `(1, 5, 3, 19, 18, 25); ` `$n` `= sizeof(` `$arr` `); ` `echo` `"Minimum difference is "` `, ` ` ` `findMinDiff(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Minimum difference is 1

**Method 2 (Efficient: O(n Log n)**

The idea is to use sorting. Below are steps.

1) Sort array in ascending order. This step takes O(n Log n) time.

2) Initialize difference as infinite. This step takes O(1) time.

3) Compare all adjacent pairs in sorted array and keep track of minimum difference. This step takes O(n) time.

Below is implementation of above idea.

## C++

`// C++ program to find minimum difference between ` `// any pair in an unsorted array ` `#include <bits/stdc++.h> ` `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[0]); ` ` ` `cout << ` `"Minimum difference is "` `<< findMinDiff(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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 method to test the above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = ` `new` `int` `[]{` `1` `, ` `5` `, ` `3` `, ` `19` `, ` `18` `, ` `25` `}; ` ` ` `System.out.println(` `"Minimum difference is "` `+ ` ` ` `findMinDiff(arr, arr.length)); ` ` ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python 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 ` `arr ` `=` `[` `1` `, ` `5` `, ` `3` `, ` `19` `, ` `18` `, ` `25` `] ` `n ` `=` `len` `(arr) ` `print` `(` `"Minimum difference is "` `+` `str` `(findMinDiff(arr, n))) ` ` ` `# This code is contributed by Pratik Chhajer ` |

*chevron_right*

*filter_none*

## 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}; ` ` ` `Console.WriteLine(` `"Minimum difference is "` `+ ` ` ` `findMinDiff(arr, arr.Length)); ` ` ` ` ` `} ` `} ` ` ` `//This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum ` `// difference between any pair ` `// in an unsorted array ` ` ` `// Returns minimum difference ` `// between any pair ` `function` `findMinDiff(` `$arr` `, ` `$n` `) ` `{ ` ` ` `// Sort array in ` `// non-decreasing order ` `sort(` `$arr` `); ` ` ` `// Initialize difference ` `// as infinite ` `$diff` `= PHP_INT_MAX; ` ` ` `// Find the min diff by ` `// comparing adjacent ` `// pairs in sorted array ` `for` `(` `$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 ` `$arr` `= ` `array` `(1, 5, 3, 19, 18, 25); ` `$n` `= sizeof(` `$arr` `); ` `echo` `"Minimum difference is "` `, ` ` ` `findMinDiff(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Minimum difference is 1

### Asked in: Amazon

This article is contributed by **Harshit Agrawal**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find minimum difference between any two elements | Set 2
- Find an index such that difference between product of elements before and after it is minimum
- Minimize the difference between minimum and maximum elements
- Minimum absolute difference of adjacent elements in a circular array
- Choose k array elements such that difference of maximum and minimum is minimized
- Find set of m-elements with difference of any two elements is divisible by k
- Find k ordered pairs in array with minimum difference d
- Find maximum difference between nearest left and right smaller elements
- Find the difference of count of equal elements on the right and the left for each element
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- 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
- Find minimum changes required in an array for it to contain k distinct elements
- Flip minimum signs of array elements to get minimum sum of positive elements possible