Java Program to Find 2 Elements in the Array such that Difference Between them is Largest


In this problem, we will find two array elements such that the difference between them is maximum.

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 element 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.

Sample Examples

Input

array[] = { 50, 10, 30, 400, 600, 200, 70, 90, 80, 322 }

Output

10, 600

Explanation

The maximum difference between 600 and 10 is 590.

Input

array[] = {1000, 30, 5000, 476, 987, 312 }

Output

30, 5000

Explanation

The maximum difference is between 30 and 5000.

Input

array[] = {-70, -150, 40, 500, -90 }

Output

150, 500

Explanation

The smallest element in the array is -150, and the largest element is 500.

Approach 1

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.

Algorithm

  • Step 1 − Initialize the ‘curr_diff’ with 0 to store the difference between two elements, 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.

  • Step 2 − Start traversing the array from the 0th index. Also, use a nested loop to traverse the array from the p + 1 index.

  • Step 3 − Take the absolute difference of the array elements, which are at pth and the qth, index and store them into the curr_diff.

  • Step 4 − If curr_diff is greater than max_diff, update the value of max_diff, first, and second variable.

  • Step 5 − 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.

Approach 2

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.

Algorithm

  • Step 1 − Use the Arrays.sort() method to sort the array.

  • Step 2 − Take the difference between nums[arr_len - 1] and nums[0].

  • Step 3 − 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.

Approach 3

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.

Algorithm

  • Step 1 − initialize the maxi and mini with the first array element to store the maximum and minimum elements.

  • Step 2 − Start iterating the array.

  • Step 3 − If the current array element exceeds maxi, update the maxi. Also, update the mini if the current element is less than the mini.

  • Step 4 − Take the difference between the maxi and mini.

  • Step 5 − 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.

Updated on: 04-Jul-2023

280 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements