-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProccessData.java
77 lines (56 loc) · 2.21 KB
/
ProccessData.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
TCS - NQT - YOP 2024
Proccess data of candidates with name and score;
sort by score
and display k th number of candidates on top of list
input:
Enter K value:
3
Enter number of Candidates:
5
Enter name of candidate 1 :
Sofiyan
Enter score of candidate 1 :
92
Enter name of candidate 2 :
Pawan
Enter score of candidate 2 :
90
Enter name of candidate 3 :
Shibban
Enter score of candidate 3 :
91
Enter name of candidate 4 :
John
Enter score of candidate 4 :
85
*/
import java.util.*;
public class ProccessData {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter K value:");
int k = scanner.nextInt();
System.out.println("Enter number of Candidates:");
int numCandidates = scanner.nextInt();
scanner.nextLine(); // Consume newline character
Map<String, Integer> candidates = new HashMap<>();
for (int i = 0; i < numCandidates; i++) {
System.out.println("Enter name of candidate " + (i + 1) + ":");
String name = scanner.nextLine();
System.out.println("Enter score of candidate " + (i + 1) + ":");
int score = scanner.nextInt();
scanner.nextLine(); // Consume newline character
candidates.put(name, score);
}
// Sorting candidates by score (descending order)
List<Map.Entry<String, Integer>> sortedCandidates = new ArrayList<>(candidates.entrySet());
Collections.sort(sortedCandidates, (a, b) -> b.getValue() - a.getValue());
// Displaying the top k candidates
System.out.println("\nTop " + k + " candidates:");
for (int i = 0; i < Math.min(k, sortedCandidates.size()); i++) {
Map.Entry<String, Integer> entry = sortedCandidates.get(i);
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}