forked from super30admin/Array-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem2.java
59 lines (52 loc) · 1.55 KB
/
Problem2.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
57
58
59
// Time Complexity : O(n)
// Space Complexity : O(1)
// Your code here along with comments explaining your approach
/**
* The idea is that when n is odd then initialize min and max as the first element.
* If n is even then initialize min and max as minimum and maximum of the first two
* elements respectively. For the rest of the elements, pick them in pairs and compare
* their maximum and minimum with max and min respectively.
*/
public class Problem2 {
static class Pair {
int min;
int max;
}
static Pair getMinMax(int arr[], int n) {
Pair minmax = new Pair();
int i;
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;
} else {
minmax.min = arr[0];
minmax.max = arr[0];
i = 1;
}
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;
}
return minmax;
}
}