1. You are given a number n, representing the count of elements.
2. You are given n numbers.
3. You are required to find the span of input. Span is defined as difference of maximum value and minimum value.
Input Format
A number n
n1
n2
.. n number of elements
Output Format
A number representing max - min
Constraints
1 <= n <= 10^4
0 <= n1, n2
.. n elements <= 10 ^9
Sample Input
6
15
30
40
4
11
9
Sample Output
36
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception {
// write your code here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
for(int i=0;i<n;i++){
if(arr[i]<min)
min = arr[i];
else if(arr[i] > max)
max = arr[i];
}
System.out.println(max-min);
}
}
Comments
Post a Comment