-
Notifications
You must be signed in to change notification settings - Fork 57
/
maximum and minimum using max and min command
71 lines (64 loc) · 2.1 KB
/
maximum and minimum using max and min command
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/ Java program of above implementation
public class GFG {
/* Class Pair is used to return two values from getMinMax() */
static class Pair {
int min;
int max;
}
static Pair getMinMax(int arr[], int n) {
Pair minmax = new Pair();
int i;
/* If array has even number of elements then
initialize the first two elements as minimum and
maximum */
if (n % 2 == 0) {
if (arr[0] > arr[1]) {
minmax.max = arr[0];
minmax.min = arr[1];
} else {
minmax.min = arr[0];
minmax.max = arr[1];
}
i = 2;
/* set the starting index for loop */
} /* If array has odd number of elements then
initialize the first element as minimum and
maximum */ else {
minmax.min = arr[0];
minmax.max = arr[0];
i = 1;
/* set the starting index for loop */
}
/* In the while loop, pick elements in pair and
compare the pair with max and min so far */
while (i < n - 1) {
if (arr[i] > arr[i + 1]) {
if (arr[i] > minmax.max) {
minmax.max = arr[i];
}
if (arr[i + 1] < minmax.min) {
minmax.min = arr[i + 1];
}
} else {
if (arr[i + 1] > minmax.max) {
minmax.max = arr[i + 1];
}
if (arr[i] < minmax.min) {
minmax.min = arr[i];
}
}
i += 2;
/* Increment the index by 2 as two
elements are processed in loop */
}
return minmax;
}
/* Driver program to test above function */
public static void main(String args[]) {
int arr[] = {1000, 11, 445, 1, 330, 3000};
int arr_size = 6;
Pair minmax = getMinMax(arr, arr_size);
System.out.printf("\nMinimum element is %d", minmax.min);
System.out.printf("\nMaximum element is %d", minmax.max);
}
}