Java Program to Find the Difference between Largest and Smallest Element of an Array of Integers

In the previous article, we have seen Java Program to Move All the 0’s (zero elements) to the End of the Array

In this article we will see how to find the difference between the largest element and smallest element of an array of integers.

Java Program to Find the Difference between Largest and Smallest Element of an Array of Integers

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find the difference between the largest element and smallest element of an array of integers.

Method-1: Java Program to Find the Difference between Largest and Smallest Element of an Array of Integers By Static Initialization of Array Elements

Approach:

  1. Declare and initialize an array of integers.
  2. Initialize two variables, large = Integer.MIN_VALUE and small = Integer.MAX_VALUE.
  3. Iterate over the array to find the largest and the smallest elements and store them in variables large and small respectively.
  4. Print the difference between large and small.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
        System.out.println("The array is : ");
        printArray(arr);
        //Calling the printDifference() method to find 
        //the difference between largest and smallest element of array
        printDifference(arr);
    }

    static void printDifference(int[] arr) 
    {
        // initialize large and small variables
        int large = Integer.MIN_VALUE;
        int small = Integer.MAX_VALUE;
        // iterate over the array to 
        //ind the largest and the smallest elements
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > large) {
                large = arr[i];
            }
            if (arr[i] < small) {
                small = arr[i];
            }
        }
        // print the difference largest, smallest element 
        //and the difference between them
        System.out.println("Largest element in array : "+large);
        System.out.println("Smallest element in array : "+small);
        System.out.println("The difference: " + (large - small));
    }
    
    //printArray() method to print the array
    static void printArray(int[] arr)
    {
        // printing array
        for (int i=0; i<arr.length; i++) 
        {
            System.out.print(arr[i] + " ");
        }
        System.out.println("");
    }
}
Output:

The array is : 
1 2 3 4 5 6 7 8 9 10 11 
Largest element in array : 11
Smallest element in array : 1
The difference: 10

Method-2: Java Program to Find the Difference between Largest and Smallest Element of an Array of Integers By Dynamic Initialization of Array Elements

Approach:

  1. Take input of array size from user.
  2. Then take input of array elements from user.
  3. Initialize two variables, large = Integer.MIN_VALUE and small = Integer.MAX_VALUE.
  4. Iterate over the array to find the largest and the smallest elements and store them in variables large and small respectively.
  5. Print the difference between large and small.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }

        System.out.println("The array is : ");
        printArray(arr);
        //Calling the printDifference() method to find 
        //the difference between largest and smallest element of array
        printDifference(arr);
    }

    static void printDifference(int[] arr) 
    {
        // initialize large and small variables
        int large = Integer.MIN_VALUE;
        int small = Integer.MAX_VALUE;
        // iterate over the array to 
        //ind the largest and the smallest elements
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > large) {
                large = arr[i];
            }
            if (arr[i] < small) {
                small = arr[i];
            }
        }
        // print the difference largest, smallest element 
        //and the difference between them
        System.out.println("Largest element in array : "+large);
        System.out.println("Smallest element in array : "+small);
        System.out.println("The difference: " + (large - small));
    }
    
    //printArray() method to print the array
    static void printArray(int[] arr)
    {
        // printing array
        for (int i=0; i<arr.length; i++) 
        {
            System.out.print(arr[i] + " ");
        }
        System.out.println("");
    }
}
Output:

Enter the size of array: Enter array elements: The array is : 
2 5 1 6 4 
Largest element in array : 6
Smallest element in array : 1
The difference: 5

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: