Jersey Number – Highest in Team
Find the highest jersey number in each team when students are divided into teams of size T with remaining students distributed round-robin
Understand the Problem
Problem Statement
In a school there are N students standing in a straight line. Their Jersey numbers from left to right are passed as the input. The students are to be divided into teams of size T starting from left. First T students form Team 1, next T students form Team 2 and so on. If N is not divisible by T and say the remainder is R, the R students are distributed among the teams formed so far in a round robin fashion (starting from the first team) until the R students are assigned to one of the teams.
Once the teams are formed, the program must print the highest jersey number in each team.
Constraints
- 1 <= N <= 9999999
- Team size T must be greater than 0
- Jersey numbers are positive integers
- When N is not divisible by T, the remainder students are distributed in round-robin fashion
Examples
10
1 5 8 2 4 11 20 6 7 9
58 20With team size 5, first 5 students form Team 1 (max = 8) and next 5 form Team 2 (max = 20). No remainder students, so output is '8 20'.
10
1 5 8 2 4 11 3 6 9 7
49 11With team size 4, first 4 students form Team 1 (max = 8), next 4 form Team 2 (max = 11). Remainder = 2, so students 9 and 7 are distributed: student 9 to Team 1 (new max = 9), student 7 to Team 2 (max remains 11). Final output: '9 11'.
Solution
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
int *jerseyNumbers = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d", &jerseyNumbers[i]);
}
int t;
scanf("%d", &t);
int numTeams = n / t;
int remainder = n % t;
// Initialize team maxima with first member of each team
int *teamMax = (int*)malloc(numTeams * sizeof(int));
for (int i = 0; i < numTeams; i++) {
teamMax[i] = jerseyNumbers[i * t];
}
// Find max in each team's base members
for (int team = 0; team < numTeams; team++) {
for (int member = 0; member < t; member++) {
int index = team * t + member;
if (jerseyNumbers[index] > teamMax[team]) {
teamMax[team] = jerseyNumbers[index];
}
}
}
// Distribute remaining students round-robin
for (int i = 0; i < remainder; i++) {
int studentIndex = numTeams * t + i;
int teamIndex = i; // Round-robin distribution
if (jerseyNumbers[studentIndex] > teamMax[teamIndex]) {
teamMax[teamIndex] = jerseyNumbers[studentIndex];
}
}
// Print results
for (int i = 0; i < numTeams; i++) {
printf("%d ", teamMax[i]);
}
free(jerseyNumbers);
free(teamMax);
return 0;
}C Solution Explanation:
- Allocate memory for jersey numbers array
- Read input values for N, jersey numbers, and team size T
- Calculate number of complete teams and remainder students
- Initialize team maxima array with first member of each team
- Loop through each team and find maximum among T base members
- For remaining students, distribute them round-robin and update team maxima if new student has higher number
- Print all team maxima separated by spaces
- Free allocated memory before exit