-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.minAndMaxOfArrays.java
56 lines (41 loc) · 1.3 KB
/
2.minAndMaxOfArrays.java
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
46
47
48
49
50
51
52
53
54
55
56
/*package whatever //do not write package name here */
//gfg link : https://practice.geeksforgeeks.org/problems/maximum-and-minimum-of-array-elements/0
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static int findMin(int arr[]){
int min = Integer.MAX_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i]<min)
min = arr[i];
}
return min;
}
public static int findMax(int arr[]){
int max = Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i]>max)
max = arr[i];
}
return max;
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int size = sc.nextInt();
int arr[] = new int[size];
for(int i=0;i<size;i++){
arr[i] = sc.nextInt();
}
System.out.println(findMax(arr)+" "+findMin(arr));
// //method : 2 simple but o(nlogn)
// Arrays.sort(arr);
// if(arr.length==1)
// System.out.println(arr[0]+" "+arr[0]);
//else
// System.out.println(arr[arr.length-1]+" "+arr[0]);
}
}
}