Java Program to Find 2 Elements in the Array such that Difference Between them is Largest
Last Updated :
26 Mar, 2021
An array is the most efficient data structure that is designed to store and access a group of objects. Given an array of integers, our task is to find out two elements from that array such that the difference between them is the maximum.
We will be discussing two approaches:
- By comparing and checking the difference between every possible pair by running two loops.
- By calculating the min and max values of the array and returning the difference between them.
Input : arr = {2, 3, 10, 6, 4, 8, 1}
Output : Two elements with largest difference: 10 and 1
Explanation : The maximum difference is between 10 and 1.
Input : arr = {-7, 9, 5, 6, 3, 2}
Output : Two elements with largest difference: 9 and -7
Explanation : The maximum difference is between 9 and -7.
Input : arr = {10, 11, 88, 2, 12, 120}
Output : Two elements with largest difference: 2 and 120
Explanation : The maximum difference is between 2 and 120.
Method 1: By comparing and checking the difference between every possible pair by running two loops
- Use two loops. In the outer loop, pick elements one by one, and in the inner loop calculate the difference of the picked element with every other element in the array.
- And simultaneously compare the difference with the maximum difference calculated so far.
- Check for all possible differences between any 2 elements in the array and finally select the elements whose difference is the largest.
Below is the implementation of the above approach :
Java
// Java Program to Find 2
// Elements in an array
// such that the difference
// between them is the largest
import java.io.*;
class Largest_Difference_GFG {
public static int[] Maximum_Diff(int a[], int n)
{
int diff, greatest_diff = a[1] - a[0];
int ele1 = a[1], ele2 = a[0];
// Array to store the difference and the
// two elements ele1 and ele2 .
int res[] = new int[3];
// 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++) {
diff = Math.abs(a[i] - a[j]);
if (diff > greatest_diff) {
greatest_diff = diff;
ele1 = a[i];
ele2 = a[j];
}
}
}
res[0] = greatest_diff;
res[1] = ele1;
res[2] = ele2;
return (res);
}
public static void main(String[] args)
{
int arr[] = { 10, 11, 88, 2, 12, 120 };
int size = arr.length;
int[] result;
result = Largest_Difference_GFG.Maximum_Diff(arr,
size);
System.out.println("Greatest Difference:"
+ result[0]);
System.out.println(
"Two elements with largest difference: "
+ result[1] + " and " + result[2]);
}
}
OutputGreatest Difference:118
Two elements with largest difference: 2 and 120
- Time Complexity: O(n^2)
- Auxiliary Space: O(1)
Method 2: By calculating the min and max values of the array and returning the difference between them
- The Largest difference between 2 elements in an array will be always the absolute difference between the smallest and largest element present in that array.
- Using 2 independent for loops, determine the min and max element of the array. Below is the implementation of the above approach :
Java
// Java Program to Find 2
// Elements in an array
// such that the difference
// between them is the largest
import java.util.*;
class GFG {
public static void main(String args[])
{
int array[] = new int[] { 10, 11, 88, 2, 12, 120 };
int len = array.length;
Arrays.sort(array); // sorting the array
int max_diff = array[len - 1] - array[0];
System.out.println("Maximum Difference is: "
+ max_diff);
System.out.println(
"Two elements with largest difference: "
+ array[0] + " and " + array[len - 1]);
}
}
OutputMaximum Difference is: 118
Two elements with largest difference: 2 and 120
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Note: Both Minimum and Maximum element in the above code can also be determined in a single for loop by comparing every element of an array with the minValue and maxValue and update them accordingly. In this way, a single for loop can return both the minValue and maxValue, and the lines of code will be reduced.
Similar Reads
Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java.Example Input/Output:Input: arr = { 1, 2, 3, 4, 5}Output: 5Input: arr = { 10, 3, 5, 7, 2, 12}Ou
4 min read
Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort()
2 min read
Java Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example :  Input: arr[] = {1, 14, 2, 16, 10, 20} Output: The third Largest element is 14 Explanation: Largest element is 20, second largest element is 16 and third largest element is 1
5 min read
Java Program to Maximize count of corresponding same elements in given Arrays by Rotation Given two arrays arr1[] and arr2[] of N integers and array arr1[] has distinct elements. The task is to find the maximum count of corresponding same elements in the given arrays by performing cyclic left or right shift on array arr1[]. Examples:  Input: arr1[] = { 6, 7, 3, 9, 5 }, arr2[] = { 7, 3,
3 min read
Java Program for Maximum difference between groups of size two Given an array of even number of elements, form groups of 2 using these array elements such that the difference between the group with highest sum and the one with lowest sum is maximum.Note: An element can be a part of one group only and it has to be a part of at least 1 group. Examples: Input : ar
3 min read