Java Program to Find Out the Minimum Difference between Two Elements Inside an Array

In the previous article, we have discussed about Java Program to Find All the Elements in an Array that are Smaller than All Elements to Their Left

In this article we are going to see how we can find out the minimum difference between two elements inside an array by using Java programming language.

Java Program to Find Out the Minimum Difference between Two Elements Inside an Array

Here we will find minimum difference between two elements in an array.

For example:

There is an array arr={ 1, 3, 5, 4, 8, 2, 4, 3, 6, 5 }

Example-1

Two elements are
x=5 and y=8
We can see 5 is present at index 2 and 9
While 8 is present at index 4
So the minimum difference between element 5 and 8 is 2 (i.e.4-2=2)

Example-2

Two elements are
x=4 and y=8
We can see 4 is present at index 3 and 6
While 8 is present at index 4
So the minimum difference between element 4 and 8 is 1 (i.e.4-3=1)

Approach:

  • Create an array of elements and two variables with the elements we want to check.
  • Create a user defined function that takes the array and the two elements as arguments. Traverse through the array and if we encounter the variables x and y then set the x_/y iterator to that index and calculate the minimum difference using the Integer.min( ) function.
  • Print the minimum difference.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args) 
    {
        int[] arr = { 1, 3, 5, 4, 8, 2, 4, 3, 6, 5 };
        int x = 8, y = 3;
        // Calls the minDifference function
        int minimumDiff = minDifference(arr,x,y);
        // Print the minimum difference
        // If the minimum difference is equal to MAX_VALUE then it is invalid
        if(minimumDiff!=Integer.MAX_VALUE)
            System.out.println("The minimum difference is " + minimumDiff);
        else
            System.out.println("Invalid!");
    }
    // User defined function to calculate the minimum difference between two elements
    public static int minDifference(int[] arr, int x, int y)
    {
        int x_iter = arr.length, y_iter = arr.length, min_diff = Integer.MAX_VALUE;
        // traverse throgh the array
        for (int i = 0; i < arr.length; i++)
        {
            // if the current element is `x`
            if (arr[i] == x)
            {
                // set `x_iter` to the current index
                x_iter = i;
                if (y_iter != arr.length)
                {
                    min_diff = Integer.min(min_diff, Math.abs(x_iter - y_iter));
                }
            }
            // if the current element is y
            if (arr[i] == y)
            {
                // set `y_iter` to the current index
                y_iter = i;
                if (x_iter != arr.length)
                {
                    min_diff = Integer.min(min_diff, Math.abs(x_iter - y_iter));
                }
            }
        }
        return min_diff;
    }
}

Output:

The minimum difference is 3

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: