
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Two Elements in Array with Largest Difference
In this problem, we will find two array elements such that the difference between them is maximum using Java.
We can make a pair of each element and find the difference between the elements of each pair. After that, we can take a pair whose element has a maximum difference. Another approach is to sort the array and take the largest and smallest elements from the array.
Problem statement
We have given an array containing the integer values. We need to find two array elements to maximize the difference between them.
Input 1
array[] = { 50, 10, 30, 400, 600, 200, 70, 90, 80, 322 }
Output 1
The maximum difference between 600 and 10 is 590.
Input 2
array[] = {1000, 30, 5000, 476, 987, 312 }
Output 2
The maximum difference is between 30 and 5000.
Input 3
array[] = {-70, -150, 40, 500, -90 }
Output 3
The smallest element in the array is -150, and the largest element is 500.
Different Approaches
Following are the different approaches to find 2 elements in the array
Using the Brute Force Approach
In this approach, we will create pairs containing two array elements. After that, we will take both elements' differences and update the maximum difference value if required.
-
Initialize the ?curr_diff' with 0 to store the difference between two elements and the ?max_diff' with a difference of the first and second elements. Also, initialize the ?first' and ?second' variables to store the pair of elements with a maximum difference.
-
Start traversing the array from the 0th index. Also, use a nested loop to traverse the array from the p + 1 index.
-
Take the absolute difference of the array elements, which are at pth and the qth, index and store them into the curr_diff.
-
If curr_diff is greater than max_diff, update the value of max_diff, first, and second variable.
-
Print the maximum difference, first and second variable's value.
Example
import java.io.*; public class Main { public static void getMaxDiff(int array[], int arr_len) { // Variable initialization int curr_diff, max_diff = array[1] - array[0]; int first = array[1], second = array[0]; // Traverse the array and find the difference between each 2 elements for (int p = 0; p < arr_len; p++) { for (int q = p + 1; q < arr_len; q++) { curr_diff = Math.abs(array[p] - array[q]); // If the difference between two elements is greater than max_diff, update max_diff. if (curr_diff > max_diff) { max_diff = curr_diff; first = array[p]; second = array[q]; } } } System.out.println("Maximum difference is " + max_diff + " between " + first + " and " + second); } public static void main(String[] args) { int array[] = { 50, 10, 30, 400, 600, 200, 70, 90, 80, 322 }; int arr_len = array.length; getMaxDiff(array, arr_len); } }
Output
Maximum difference is 590 between 10 and 600
Time complexity : O(N^2) to create pair of each element.
Space complexity : O(1) as we don't use any dynamic space.
Using Sorting
In this approach, we will sort the array. After that, we can take the first and last element of the sorted array to get the maximum difference between any two array elements.
- First import necessary classes for the java.io and java.util package.
- Use the Arrays.sort() method to sort the array.
- Take the difference between nums[arr_len - 1] and nums[0].
- Print the difference, nums[arr_len - 1] and nums[0] in the output.
Example
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { int nums[] = { 50, 10, 30, 400, 600, 200, 70, 90, 80, 322 }; int arr_len = nums.length; // sort array Arrays.sort(nums); // Get the max difference int max_diff = nums[arr_len - 1] - nums[0]; // print max difference System.out.println("Maximum difference is " + max_diff + " between " +nums[0] + " and " + nums[arr_len - 1] ); } }
Output
Maximum difference is 590 between 10 and 600
Time complexity : O(NlogN) to sort the array.
Space complexity : O(N) for sorting the array.
Using Optimized Approach
In this approach, we will traverse the array to find the maximum and minimum elements from the given array. After that, we can take the minimum and maximum element differences, which will be the largest difference.
-
Initialize the maxi and mini with the first array element to store the maximum and minimum elements.
-
Start iterating the array.
-
If the current array element exceeds maxi, update the maxi. Also, update the mini if the current element is less than the mini.
-
Take the difference between the maxi and mini.
-
Print the difference between maxi and mini to show in the output.
Example
import java.io.*; public class Main { public static void getMaxDiff(int array[], int arr_len) { int maxi = array[0]; int mini = array[0]; // Finding the maximum and minimum element in the array for (int p = 0; p < arr_len; p++) { if (array[p] > maxi) { maxi = array[p]; } if (array[p] < mini) { mini = array[p]; } } // Getting the maximum difference int max_diff = maxi - mini; System.out.println("Maximum difference is " + max_diff + " between " + mini + " and " + maxi); } public static void main(String[] args) { int array[] = { -70, -150, 40, 500, -90 }; int arr_len = array.length; getMaxDiff(array, arr_len); } }
Output
Maximum difference is 650 between -150 and 500
Time complexity : O(N) to traverse the array.
Space complexity : O(1), as we don't use extra space.
We have seen three approaches to find two elements with the largest difference. We can only get the largest difference between two array elements when we take the difference between the smallest and largest array elements. The third approach provides the most optimized solution in the sense of time and space complexity.