Write a java program to find Largest Difference.

Write a program to read an integer array, find the largest difference between adjacent elements and display the index of the largest difference.
EXAMPLE:
input1: {2,4,5,1,9,3,8}
output1: 4 (here largest difference 9-1=8 then return index of 9 ie,4)

Input and Output Format

  • Input consists of an integer ‘n’ which is the number of elements followed by n integer values.
  • The output consists of the integer.

Refer sample output for formatting specifications

Sample Input 1:
7
2
4
5
1
9
3
8
Sample Output 1:
4

Program to Find Largest Difference in Java

Following are the steps to find the largest difference in an array:

  • Input the size of an array from the user.
  • Create an array and add elements to it. Pass it to the getDiffArray() method.
  • Inside the method, declare and initialize variables n2, n3, and n4.
  • Itreare over the array using for loop, and check the absolute difference between adjacent elements and store it in variable n2.
  • For each iteration check the value of n2, if it is greater than n3 transfer the value of n2 to n3 and assign the index value to n4 and return it to the user.
import java.util.Scanner;

public class Main {
  public static int getDiffArray(int[] n1) {
    int n2, n3 = 0, n4 = 0, i;
    for (i = 0; i < n1.length - 1; i++) {
      n2 = Math.abs(n1[i] - n1[i + 1]);
      if (n2 > n3) {
        n3 = n2;
        n4 = i + 1;
      }
    }
    return n4;
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int a[] = new int[20];
    for (int i = 0; i < n; i++) {
      a[i] = sc.nextInt();
    }

    System.out.println(getDiffArray(a));
  }
}

Output

Display Largest Difference in an array

Write a code to print the largest difference among the elements in an array. Let us consider we have an array of integers, you just need to print the largest difference between any two elements.

Input and Output Format

  • Input consists of an integer ‘n’ which is the number of elements followed by n integer values.
  • The output consists of the integer.

Refer sample output for formatting specifications

Sample Input 1:
5
6
5
9
10
15
Sample Output 1:
10 (15-5)

Following are the steps to find the largest difference in an array:

  • Input the size of an array from the user.
  • Create an array and add elements to it. Pass it to the getDiffArray() method.
  • Inside the method, first, get the difference between the first and second elements and store it in the n2 variable.
  • Iterate over an array using two loops, in the outer loop, select elements one by one, and in the inner loop calculate the difference of the selected element with every other element in the array.
  • Then, compare the difference with the largest difference calculated(i.e value in n2). If it is greater then store it in the n2 variable.
  • At last, return the value of n2.
package com.demo2;

import java.util.Scanner;

public class Test {
    public static int getDiffArray(int[] n1) {
     int n2 = n1[1] - n1[0];
     for(int i = 0;i<n1.length;i++){
         for(int j = i+1;j<n1.length;j++){
             if(n1[j]-n1[i] > n2){
                 n2 = n1[j]-n1[i];
             }
         }
     }
     return n2;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[] = new int[20];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        System.out.println(getDiffArray(a));

    }
}

Output

Thus, in this, way, we can find out the difference between the largest numbers in an array.