LAST UPDATED: MARCH 5, 2021
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
- Start
- Declare the array size.
- Ask the user to initialize the array size.
- Declare the array.
- Ask the user to initialize the array elements.
- Use two for loops for the same.
- Use the first for loop to hold the elements.
- Use the second for loop to calculate the difference between the picked element with every other element in the array.
- Simultaneously compare the difference with the maximum difference calculated.
- Check for all the possible differences between any two 2 elements.
- Print the elements with the largest difference.
- 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[1] - arr[0];
int element1 = arr[1], element2 = arr[0]; // two elements ele1 and ele2 .
int res[] = new int[3]; // 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[0] = largestDifference;
res[1] = element1;
res[2] = element2;
System.out.println( "Two elements with largest difference are "
+ res[1] + " and " + res[2]);
System.out.println("The Greatest Difference is "
+ res[0]);
}
}
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
- Start
- Declare the array size.
- Ask the user to initialize the array size.
- Declare the array.
- Ask the user to initialize the array elements.
- Sort the array using Array.sort().
- The first and last elements will be the minimum and maximum elements in the array.
- The difference between these two elements will give the largest element.
- Print the elements with the largest difference.
- 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[0]);
int difference=arr[n-1]-arr[0];
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