DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Finding a Friend's Name

Back to problem

Editorial

Let the missing student's name be xxx.

If n>1n>1n>1, the student immediately before xxx in Class 6 must be an−1a_{n-1}an−1​, so an−1<xa_{n-1}<xan−1​<x. If n<mn<mn<m, the student immediately after xxx must be ana_nan​, so x<anx<a_nx<an​.

Therefore, for each name xxx, it is sufficient to check the following two conditions:

  • If n>1n>1n>1, then an−1<xa_{n-1}<xan−1​<x.
  • If n<mn<mn<, then .

The complete second-grade roster is already sorted. Collecting valid names in input order automatically produces the required sorted output.

The time complexity is O(m+k)O(m+k)O(m+k) per test case, and the space complexity is O(k)O(k)O(k) when the candidates are stored.

Solution written by GPT5.6

m
x<anx<a_nx<an​