Java Program to Find Maximum Difference Between Two Elements in an Array

This is a Java Program to Find 2 Elements in the Array such that Difference between them is Largest.

Enter size of array and then enter all the elements of that array. Now we check for all possible difference between two elements and then select the elements whose difference is largest one.

Here is the source code of the Java Program to Find 2 Elements in the Array such that Difference between them is Largest. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Largest_Difference 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int n, x, count = 0, i = 0, temp = 0;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter no. of elements you want in array:");
  9.         n = s.nextInt();
  10.         int a[] = new int[n];
  11.         System.out.println("Enter all the elements:");
  12.         for(i = 0; i < n; i++)
  13.         {
  14.             a[i] = s.nextInt();
  15.         }
  16.         int diff, greatest_diff;
  17.         greatest_diff = 0;
  18.         int a1 = 0, a2 = 0;
  19.         for(i = 0; i < n; i++)
  20.         {
  21.             for(int j = i + 1; j < n; j++)
  22.             {
  23.                 diff = Math.abs(a[i] - a[j]);
  24.                 if(diff > greatest_diff)
  25.                 {
  26.                     greatest_diff = diff;
  27.                     a1 = i;
  28.                     a2 = j;
  29.                 }
  30.             }
  31.         }
  32.         System.out.println("Greatest Difference:"+greatest_diff);
  33.         System.out.println("Two elements with largest difference:"+a[a1]+" and "+a[a2]);
  34.     }
  35. }

Output:

$ javac Largest_Difference.java
$ java Largest_Difference
 
Enter no. of elements you want in array:7
Enter all the elements:
-2
4
5
6
2
7
-3
Greatest Difference:10
Two elements with largest difference:7 and -3

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.