-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion1_2.java
56 lines (52 loc) · 1.28 KB
/
Question1_2.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
// Find the maximum & minimum value in an array
// Pairing Method
import java.util.*;
public class Question1_2{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int arr[] = new int[n];
int max, min, i;
// input array
for(i=0; i<=arr.length-1; i++){
arr[i]=input.nextInt();
}
if(n%2==0){
if(arr[0]>arr[1]){
max=arr[0];
min=arr[1];
}
else{
max=arr[1];
min=arr[0];
}
i=2;
}
else{
max=arr[0];
min=arr[0];
i=1;
}
while(i < n-1){
if(arr[i]>arr[i+1]){
if(arr[i]>max){
max=arr[i];
}
if(arr[i+1]<min){
min=arr[i+1];
}
}
else if(arr[i+1]>arr[i]){
if(arr[i+1]>max){
max=arr[i+1];
}
if(arr[i]<min){
min=arr[i];
}
}
i+=2;
}
System.out.println("MAX: "+max);
System.out.println("MAX: "+min);
}
}