Java ProgramsArraysFind Minimum Difference Between Two Elements

Find Minimum Difference Between Two Elements in Java

intermediate·  Arrays  ·  Array

Problem

The minimum difference is the smallest absolute gap between any two distinct elements in the array, regardless of their positions.

Given an array of integers, find the minimum absolute difference between any two of its elements.

Input
[8, 3, 15, 21, 5]
Output
Minimum difference: 2

Java Program

Java
public class MinDifference { public static void main(String[] args) { int[] arr = {8, 3, 15, 21, 5}; int minDiff = Integer.MAX_VALUE; for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { int diff = Math.abs(arr[i] - arr[j]); // order doesn't matter once absolute if (diff < minDiff) minDiff = diff; } } System.out.println("Minimum difference: " + minDiff); } }

Output

Minimum difference: 2

Core Logic

Trying every pair of elements and keeping the smallest absolute gap found checks every possible pairing directly.

How It Works
  1. 1The outer loop picks one index i, and the inner loop picks every later index j, so each pair is checked exactly once.
  2. 2Math.abs(arr[i] - arr[j]) computes the absolute difference for that pair, regardless of which value is larger.
  3. 3Whenever that difference beats minDiff, minDiff is updated to it.
  4. 4After every pair has been checked, minDiff holds the smallest gap found.
For [8, 3, 15, 21, 5], the pair (3, 5) gives an absolute difference of 2, the smallest of any pair checked.
💡

Key Point: Math.abs() is what makes the order of arr[i] and arr[j] irrelevant — the gap between two values is the same regardless of which one is subtracted from which.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: Every pair of elements is checked explicitly, so the number of comparisons grows with the square of the array's length.

Key Concepts

nested for loopbrute forceMath.abs()

Approach 2: Sorting

Java
import java.util.Arrays; public class MinDifferenceSorted { public static void main(String[] args) { int[] arr = {8, 3, 15, 21, 5}; Arrays.sort(arr); int minDiff = Integer.MAX_VALUE; for (int i = 1; i < arr.length; i++) { minDiff = Math.min(minDiff, arr[i] - arr[i - 1]); // sorted, so this is always non-negative } System.out.println("Minimum difference: " + minDiff); } }

Output

Minimum difference: 2

Core Logic

Once the array is sorted, the smallest gap between any two elements is guaranteed to sit between two neighbors — no non-adjacent pair can beat every adjacent one.

How It Works
  1. 1Arrays.sort(arr) puts the array into ascending order.
  2. 2A single pass then compares only consecutive elements, arr[i] - arr[i - 1], since the array is sorted this is always non-negative.
  3. 3Math.min(minDiff, ...) keeps the smallest adjacent gap seen so far.
  4. 4After the pass, minDiff holds the answer — checking only n - 1 adjacent pairs instead of every possible pair.
Sorting [8, 3, 15, 21, 5] gives [3, 5, 8, 15, 21]; the adjacent gaps are 2, 3, 7, and 6 — the smallest is 2, between 3 and 5.
💡

Key Point: In a sorted array, the closest two values are always next to each other — this is why only adjacent gaps need checking, instead of every one of the O(n²) possible pairs.

Complexity
Time Complexity: O(n log n)Space Complexity: O(1)

Why: Sorting the array in place costs O(n log n), and the single pass over adjacent elements afterward adds only O(n) more.

Key Concepts

Arrays.sort()adjacent comparison

Related Programs