Write Java code to return the difference between the largest and smallest elements in the array. If there is only one element in the array return the same element as output. The return type of the output is an integer which is the difference between the largest and smallest elements in the array.
Input and Output Format
- Input is an integer array.
- The first element in the input represents the number of elements in an array.
- The size of the array must be >=1.
- The output is an integer which is the difference between the largest and smallest element in an array.
Sample Input 1:
4
3
6
2
1
Sample Output 1:
5
Sample Input 2:
4
5
3
7
2
Sample Output 2:
5
Difference between largest and smallest elements in an array in Java.
Following are the steps to find the difference between the largest and smallest element in an array:
- First, input the size of the array.
- Next, initialize an array with the input elements.
- Call the display() method with the array.
- Now, inside the display() method, first sort the specified array in ascending order using Arrays.sort()
- Next, subtract the last element of the array from the first element and store it in variable n and at last return the value of n.
- If the array size is 1, then just return the element.
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int n1 = display(a); System.out.println(n1); } public static int display(int[] array) { Arrays.sort(array); int n = array[array.length - 1] - array[0]; int b = array.length; if (b == 1) { n = array[0]; } return n; } }
Output
Find Smallest and the Largest number in Java
Obtain an array, and check the smallest and largest element. Let us see the sample input and output format.
Sample Input 1:
4
3
6
2
1
Sample Output 1:
Smallest 1 and Largest 6
Java Program to find the smallest and largest number in an Array
Following are the steps to find the smallest and largest element in an array:
- Take an array as input.
- Initialize the smallest and largest variable with the starting number.
- Now, iterate over the array and update the smallest variable if we encounter a variable less than in smallest variable. Repeat this process for the largest element also.
- At, the end just print the smallest and largest element.
package com.date; import java.util.Scanner; public class LargestAndSmallestNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a[] = new int[5]; for(int i = 0 ;i < a.length; i++){ a[i] = scanner.nextInt(); } int largest = a[0]; int smallest = a[0]; for(int i = 0 ;i < a.length; i++){ if(smallest > a[i]) smallest = a[i]; if(largest < a[i]) largest = a[i]; } System.out.println("Smallest "+smallest+" Largest "+largest); } }
Output
Thus, in this way, we learn how to find the difference between the smallest and largest number. Along with finding the smallest and largest number in an array.