HackerEarth Monk’s School problem solution YASH PAL, 31 July 2024 In this HackerEarth Monk’s School problem solution Today is the 25th anniversary of Berland International School in Berland. On this auspicious Occasion, our friend Monk has been given the responsibility of preparing the Inventory for his school. There are exactly N teachers and M students in the school. Each of these teachers teaches arbitary number of students. However, each student is taught by exactly one teacher. Monk has been given the task of finding out for each teacher the Students he/she teaches. For each student Monk has been given the Student’s Name and Age. However , Monk is too busy , So he has assigned this task to us. We need to print the name of the Teacher and the Name and age of the students that he/she teaches. However, there is a catch here. We need to print the list of students of each Teacher in Lexicographical order of their names . That is list of the teacher with lexicographically smaller name will appear before other teachers with lexicographically greater names. In Addition , The students appearing in a particular teachers list should appear in Increasing order of their Age. HackerEarth Monk’s School problem solution. import java.io.*;import java.util.*;class problem_two_test{ static Scanner sc=new Scanner(System.in); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws Exception { int n=sc.nextInt(),m=sc.nextInt(); Teacher[] a=new Teacher[n]; for(int i=0;i<n;i++) { a[i]=new Teacher(sc.next(),new ArrayList<Student>()); } for(int i=0;i<m;i++) { String s1=sc.next(),s2=sc.next(); int age=sc.nextInt(); for(int j=0;j<n;j++) { if(a[j].name.equals(s1)) { a[j].al.add(new Student(s2,age)); break; } } } Arrays.sort(a); for(int i=0;i<n;i++) { out.println(a[i].name); ArrayList<Student> curr_list=a[i].al; Collections.sort(curr_list); for(Student x:curr_list) { out.println(x.name+" "+x.age); } } out.close(); }}class Teacher implements Comparable<Teacher>{ String name; ArrayList<Student> al; public Teacher(String name,ArrayList<Student> al) { this.name=name; this.al=al; } public int compareTo(Teacher x) { return this.name.compareTo(x.name); }}class Student implements Comparable<Student>{ String name; int age; public Student(String name,int age) { this.name=name; this.age=age; } public int compareTo(Student x) { return Integer.compare(this.age,x.age); }} Second solution #include <bits/stdc++.h>using namespace std;const int MAX = 105;string S[MAX];bool A[1000005];int main(){ int n, k, c; string a, b; vector<pair<int, string> >::iterator it; cin >> n >> k; assert(1 <= n and n <= 100); assert(1 <= k and k <= 100000); for(int i = 0;i < n;++i) { cin >> S[i]; for(int j = 0;j < S[i].length();++j) assert('a' <= S[i][j] and S[i][j] <= 'z'); assert(1 <= S[i].length() and S[i].length() <= 35); } sort(S, S + n); map<string, vector<pair<int, string> > > m; for(int i = 0;i < k;++i) { cin >> a >> b >> c; for(int j = 0;j < a.length();++j) assert('a' <= a[j] and a[j] <= 'z'); for(int j = 0;j < b.length();++j) assert('a' <= b[j] and b[j] <= 'z'); bool flag = true; for(int j = 0;j < n;++j) if(a == S[j]) { flag = false; break; } assert(flag == false); assert(1 <= a.length() and a.length() <= 35); assert(1 <= b.length() and b.length() <= 35); assert(1 <= c and c <= 1000000); m[a].push_back(make_pair(c, b)); } for(int i = 0;i < n;++i) { sort(m[S[i]].begin(), m[S[i]].end()); memset(A, false, sizeof(A)); cout << S[i] << endl; for(it = m[S[i]].begin();it != m[S[i]].end();++it) { cout << it->second << ' ' << it->first << endl; assert(A[it->first] == false); A[it->first] = true; } } return 0;} coding problems