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

Parity Sort

Back to problem

Editorial

Let BBB be the sorted version of AAA.

The allowed operation only swaps two odd indices or two even indices. Therefore, the multiset of values placed on odd indices and the multiset of values placed on even indices are invariant.

Conversely, if these two multisets are exactly the same as in the sorted sequence BBB, then sorting is possible. Inside the odd indices, arbitrary permutations can be achieved by swaps, and the same holds for the even indices.

Thus, collect the values at odd indices of AAA and BBB, sort and compare them. Do the same for even indices. The answer is TAK if and only if both comparisons match.

The time complexity is O(Nlog⁡N)O(N \log N)O(NlogN).