brel-logo-white brel-logo-white
  • Travel
  • Case Study
  • How to
  • Technology
  • More
    • Sports
    • Health
    • Articles
    • Review
Notification
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
BreldigitalBreldigital
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
Search
  • Quick Access
    • Home
    • History
    • My Saves
    • My Interests
    • My Feed
  • Categories
    • Travel
    • Sports
    • Health

Top Stories

Explore the latest updated news!

Stephanie puts 30 cubes in a box

Which statement correctly describes the process that occurs in the thylakoid?

Which point is on the line that passes through point h and is perpendicular to line fg?

Stay Connected

Find us on socials
248.1k Followers Like
61.1k Followers Follow
165k Subscribers Subscribe
Breldigital > Blog > ds > Maximum difference in an array java
ds

Maximum difference in an array java

By John Published November 10, 2022
Share

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.

import java.util.Scanner;
public class Largest_Difference
{
public static void main(String[] args)
{
int n, x, count = 0, i = 0, temp = 0;
Scanner s = new Scanner(System.in);
System.out.print(“Enter no. of elements you want in array:”);
n = s.nextInt();
int a[] = new int[n];
System.out.println(“Enter all the elements:”);
for(i = 0; i < n; i++) { a[i] = s.nextInt(); } int diff, greatest_diff; greatest_diff = 0; int a1 = 0, a2 = 0; for(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;
a1 = i;
a2 = j;
}
}
}
System.out.println(“Greatest Difference:”+greatest_diff);
System.out.println(“Two elements with largest difference:”+a[a1]+” and “+a[a2]);
}
}
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

TAGGED: Maximum difference in an array java
John November 10, 2022 November 10, 2022

Search

brel-logo-white brel-logo-white
Explore a wide range of brands and categories with our comprehensive coverage, and stay up-to-date with the latest news and trends by subscribing to our updates.
Categories:
  • Entertainment
  • Travel
  • Sport
  • Contact Us

Quick Links

  • My Feed
  • My Interests
  • History
  • My Saves

About US

  • Adverts
  • Our Jobs
  • Term of Use

© 2023 Brel Digital All Rights Reserved. All logos and images used on this website are registered trademarks of their respective companies

Welcome Back!

Sign in to your account

Lost your password?