-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEhabAndSubstraction.java
48 lines (45 loc) · 1.06 KB
/
EhabAndSubstraction.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
/**
* https://codeforces.com/problemset/problem/1088/B #implementation #sorting each loop (k times)
* update the minimum value and print it out.
*/
import java.util.Arrays;
import java.util.Scanner;
public class EhabAndSubstraction {
static void printNumZeroElement() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int temp = sc.nextInt();
arr[i] = temp;
}
Arrays.sort(arr);
// special case
if (k == 1) {
System.out.println(arr[0]);
return;
}
int[] resultArr = new int[k];
resultArr[0] = arr[0];
int minVal = arr[0];
int count = 1;
for (int i = 1; i < n; i++) {
if (arr[i] - minVal > 0) {
resultArr[count] = arr[i] - minVal;
minVal = arr[i];
count++;
}
if (count == k) {
break;
}
}
// result
for (int i : resultArr) {
System.out.println(i);
}
}
public static void main(String[] args) {
printNumZeroElement();
}
}