This is the Java Program to Find the Minimum Distance between Array Elements.

Given an array of n elements, and two integers say x and y, present in the array, find out the minimum distance between x and y in the array, that is the number of elements between x and y, including y. If the elements are not present print infinite value.

Example:

ArrayOne = [1, 2, 3, 4, 5, 6]

x = 2

y = 5

Output

3

**Check for the first occurrence of x or y in the array** and store that index in a variable say z. Now continue traversing the array, if you find x or y, and it is not equal to the element at z, then update the minimum distance. Finally, update the index in variable z.

Here is the source code of the Java Program to Find the Minimum Distance between Array Elements. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

`// Java Program to Find the Minimum Distance between Array Elements`

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class MinimumDistance {

`// Function to calculate the minimum distance`

static int minimumDistance(int[] array,int x,int y){

int prev,i=0;

prev = 0;

int dist = Integer.MAX_VALUE;

for(i=0; i<array.length; i++){

if(array[i] == x || array[i] == y){

prev = i;

break;

`}`

`}`

for(i=prev+1; i<array.length; i++){

if(array[i] == x || array[i] == y){

if(array[i] != array[prev] && (i-prev) < dist)

`{`

dist = i-prev;

prev = i;

`}`

else{

prev = i;

`}`

`}`

`}`

return dist;

`}`

`// Function to read input and display the output`

public static void main(String[] args) {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int size;

System.out.println("Enter the size of the array");

try {

size = Integer.parseInt(br.readLine());

} catch (Exception e) {

System.out.println("Invalid Input");

return;

`}`

int[] array = new int[size];

System.out.println("Enter array elements");

int i;

for (i = 0; i < array.length; i++) {

try {

array[i] = Integer.parseInt(br.readLine());

} catch (Exception e) {

System.out.println("An error Occurred");

`}`

`}`

int x,y;

System.out.println("Enter the numbers between which" +

" the distance is to be calculated");

try {

x = Integer.parseInt(br.readLine());

y = Integer.parseInt(br.readLine());

} catch (Exception e) {

System.out.println("Invalid Input");

return;

`}`

int output = minimumDistance(array,x,y);

System.out.println("The minimum distance is " + output);

`}`

`}`

1. In function minimumDistance(), the variable dist is initialized to Integer.MAX_VALUE.

2. The first loop for(i=0; i<array.length; i++) is used to find the first occurrence of either element x or y and store that index in variable prev.

3. The second loop for(i=prev+1; i<array.length; i++) traverses through the remaining part.

4. The conditions if(array[i] == x || array[i] == y) { if(array[i] != array[prev] && (i-prev) < dist), first checks for x or y.

5. When found it compares the element with the element at index prev. If they are not the same, then the dist is updated accordingly.

**Time Complexity: O(n)** where n is the number of elements in the array.

Case 1 (Positive Test Case): Enter the size of the array 6 Enter array elements 1 2 3 4 5 6 Enter the numbers between which the distance is to be calculated 2 5 The minimum distance is 3 Case 2 (Negative Test Case): Enter the size of the array 5 Enter array elements 9 8 7 6 5 Enter the numbers between which the distance is to be calculated 1 2 The minimum distance is 2147483647 Case 3 (Positive Test Case - another example): Enter the size of the array 8 Enter array elements 12 2 45 57 7 54 3 2 Enter the numbers between which the distance is to be calculated 2 3 The minimum distance is 1

**Sanfoundry Global Education & Learning Series – Java Programs.**

**Next Steps:**

- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Practice BCA MCQs
- Apply for Java Internship
- Apply for Information Technology Internship
- Buy Java Books
- Practice Programming MCQs