DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
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 account linking.

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

Sorting a Sequence 3

Back to problem

Editorial

Only cyclic shifts are reachable. Count cyclic descents, including the comparison from ANA_NAN​ to A1A_1A1​. A sorted shift exists exactly when this count is at most one: cut the cycle at that descent. This takes O(N)O(N)O(N) time; checking all rotations takes O(N2)O(N^2)O(N2).