-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMinGreedy.java
More file actions
67 lines (44 loc) · 1.09 KB
/
Copy pathMaxMinGreedy.java
File metadata and controls
67 lines (44 loc) · 1.09 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
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
package rank;
import java.util.Arrays;
/**
* created by @author suraj on 24/10/19
*/
public class MaxMinGreedy {
static int maxMin(int k, int[] arr) {
Arrays.sort(arr);
int max = arr[0];
int min = arr[0];
--k;
for (int i = 1; i < arr.length; i++) {
if (k > 0) {
if (max < arr[i]) {
max = arr[i];
}
if (min > arr[i]) {
min = arr[i];
}
--k;
} else {
if (arr[i] < max) {
max = arr[i];
}
if (arr[i] < max && arr[i] > min) {
min = arr[i];
}
}
}
return max - min;
}
public static void main(String[] args) {
// 100
// 200
// 300
// 350
// 400
// 401
// 402
// int arr[] = {10, 100, 300, 200, 1000, 20, 30 } ;
int arr[] = {100, 200, 300, 350, 400, 401, 402};
System.out.println(maxMin(3, arr));
}
}