Dark Mode On/Off

# Java Program To Find two elements such that difference between them is the largest

In this tutorial, we will learn how to find 2 elements in the array such that the difference between them is largest. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java. Input: 7 8 5 4 3 2 1

Output: Largest Difference is 8-1 = 7

## Program 1: Find the Largest Difference

In this method, we will see how to find the 2 elements in the array such that the difference between them is largest by comparing and checking the difference of each pair.

### Algorithm

1. Start
2. Declare the array size.
3. Ask the user to initialize the array size.
4. Declare the array.
5. Ask the user to initialize the array elements.
6. Use two for loops for the same.
7. Use the first for loop to hold the elements.
8. Use the second for loop to calculate the difference between the picked element with every other element in the array.
9. Simultaneously compare the difference with the maximum difference calculated.
10. Check for all the possible differences between any two 2 elements.
11. Print the elements with the largest difference.
12. Stop.

The below program demonstrates how to find the 2 elements in the array such that the difference between them is largest.

``````/*Java Program to find 2 Elements in the Array such that Difference Between them is Largest*/
import java.util.*;
import java.util.Arrays;

//Driver Code
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

int n;    //Declare array size
System.out.println("Enter the total number of elements ");
n=sc.nextInt();     //Initialize array size

int arr[]=new int[n];   //Declare array
System.out.println("Enter the elements of the array ");
for(int i=0; i<n ;i++)     //Initialize array
{
arr[i]=sc.nextInt();
}

int difference, largestDifference = arr - arr;
int element1 = arr, element2 = arr;  // two elements ele1 and ele2 .

int res[] = new int; // Array to store the difference

/* Check for all possible difference between any 2 elements in the array and finally select the elements whose difference is the largest */

for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
difference = Math.abs(arr[i] - arr[j]);
if (difference > largestDifference)
{
largestDifference = difference;
element1 = arr[i];
element2 = arr[j];
}
}
}
res = largestDifference;
res = element1;
res = element2;

System.out.println( "Two elements with largest difference are "
+ res + " and " + res);
System.out.println("The Greatest Difference is "
+ res);
}
}
``````

Enter the total number of elements 10
Enter the elements of the array 7 3 5 1 3 6 8 9 5 4
Two elements with largest difference are 1 and 9
The Greatest Difference is 8

## Program 2: Find the Largest Difference

In this method, we will see how to find the 2 elements in the array such that the difference between them is largest by comparing and checking the difference of each pair.

### Algorithm

1. Start
2. Declare the array size.
3. Ask the user to initialize the array size.
4. Declare the array.
5. Ask the user to initialize the array elements.
6. Sort the array using Array.sort().
7. The first and last elements will be the minimum and maximum elements in the array.
8. The difference between these two elements will give the largest element.
9. Print the elements with the largest difference.
10. Stop

Explanation: The below program demonstrates how to find the 2 elements in the array such that the difference between them is largest.

``````/*Java Program to find 2 Elements in the Array such that Difference Between them is Largest*/
import java.util.*;
import java.util.Arrays;

//Driver Code
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

int n;    //Declare array size
System.out.println("Enter the total number of elements ");
n=sc.nextInt();     //Initialize array size

int arr[]=new int[n];   //Declare array
System.out.println("Enter the elements of the array ");
for(int i=0; i<n ;i++)     //Initialize array
{
arr[i]=sc.nextInt();
}

Arrays.sort(arr);  //Sort the array
System.out.println("Two elements with the largest difference are "+arr[n-1]+" and "+arr);
int difference=arr[n-1]-arr;
System.out.println("The largest difference is "+difference);

}
}

``````

Enter the total number of elements 10
Enter the elements of the array 8 7 6 59 7 6 42 5 7 9
Two elements with the largest difference are 59 and 5
The largest difference is 54

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.