-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathgetSmallestInefficiencies.java
More file actions
56 lines (46 loc) · 1.66 KB
/
getSmallestInefficiencies.java
File metadata and controls
56 lines (46 loc) · 1.66 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
import java.util.Scanner;
public class getSmallestInefficiencies {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many developers are (n):");
int n = sc.nextInt();
int[] skill = new int[n];
System.out.print("Enter the skills value for " + n + " developers : ");
for (int i = 0; i < n; i++) {
skill[i] = sc.nextInt();
}
System.out.print("How many inefficient skills pair you want (k) :");
int k = sc.nextInt();
int[] pair = getSmallestInefficiencies(skill, k);
System.out.println(k + " inefficient skills pair : ");
for (int i = 0; i < k; i++) {
System.out.println(pair[i]);
}
System.out.println();
}
public static int[] getSmallestInefficiencies(int[] skill, int k) {
int ln = (skill.length * (skill.length - 1)) / 2;
int[] res = new int[k];
int[] temp = new int[ln];
int t = 0;
for (int i = 0; i < skill.length; i++) {
for (int j = i + 1; j < skill.length; j++) {
temp[t] = Math.abs(skill[i] - skill[j]);
t++;
}
}
for (int i = 1; i < temp.length; i++) {
for (int j = 0; j < temp.length - i; j++) {
if (temp[j] > temp[j + 1]) {
int h = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = h;
}
}
}
for (int i = 0; i < k; i++) {
res[i] = temp[i];
}
return res;
}
}