-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpan.java
More file actions
39 lines (34 loc) · 1.07 KB
/
Span.java
File metadata and controls
39 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Span of Array
import java.io. *;
import java.util.*;
import java.util.Scanner;
public class Span{
public static int findSpan(int[] arr){
if (arr == null || arr.length == 0) {
return 0; // Return 0 for empty array
}
int min = arr[0];
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
return max - min;
}
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scn.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scn.nextInt();
}
int span = findSpan(arr);
System.out.println("The span of the array is: " + span);
}
}