-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxAndMinofAnArray.java
More file actions
45 lines (36 loc) · 1.14 KB
/
MaxAndMinofAnArray.java
File metadata and controls
45 lines (36 loc) · 1.14 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
40
41
42
43
44
45
import java.util.Scanner;
public class MaxAndMinofAnArray {
public static void main(String[] args) {
/*
* Take input an array A of size N and write a program to print maximum and minimum elements of the input.
* The only line of the input would contain a single integer N that represents the length of the array followed by the N elements of the input array A.
* */
Scanner scn = new Scanner(System.in);
int N = scn.nextInt();
int[] A = new int[N];
for(int i=0; i<A.length; i++){
A[i] = scn.nextInt();
}
System.out.println(minArr(A) + " " + maxArr(A));
}
public static int minArr(int[] arr){
int N = arr.length;
int min = Integer.MAX_VALUE;
for(int i=0; i<N; i++){
if(arr[i] < min){
min = arr[i];
}
}
return min;
}
public static int maxArr(int[] arr){
int N = arr.length;
int max = Integer.MIN_VALUE;
for(int i=0; i<N; i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
}