If N=4 and M=4 then output for one group could look like: Group X Employees: 1002,1005,2314,5120 Courses: 1202,1281,2102,4312