forked from super30admin/Array-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum-and-minimum-in-an-array.java
More file actions
42 lines (38 loc) · 1.33 KB
/
maximum-and-minimum-in-an-array.java
File metadata and controls
42 lines (38 loc) · 1.33 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
// Time Complexity : O(n)
// Space Complexity : O(1)
// Three line explanation of solution in plain english : Initialize min and max using the first one or two elements depending on array size.
// Iterate through the rest of the array in pairs, updating min and max efficiently. Return a list containing the final minimum and maximum values.
class Solution {
public static List<Integer> findMinMax(ArrayList<Integer> arr) {
List<Integer> result = new ArrayList<>();
int n = arr.size();
int mini, maxi, i;
if (n % 2 == 1) {
mini = maxi = arr.get(0);
i = 1;
} else {
if (arr.get(0) < arr.get(1)) {
mini = arr.get(0);
maxi = arr.get(1);
} else {
mini = arr.get(1);
maxi = arr.get(0);
}
i = 2;
}
// Process elements in pairs
while (i < n - 1) {
if (arr.get(i) < arr.get(i + 1)) {
mini = Math.min(mini, arr.get(i));
maxi = Math.max(maxi, arr.get(i + 1));
} else {
mini = Math.min(mini, arr.get(i + 1));
maxi = Math.max(maxi, arr.get(i));
}
i += 2;
}
result.add(mini);
result.add(maxi);
return result;
}
}