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

Keeping Multiple Positions

Back to problem

Editorial

The possible original indices of the last remaining element are exactly ⌊N/2⌋+1\lfloor N/2\rfloor+1⌊N/2⌋+1 through NNN. Output the maximum AiA_iAi​ in this range.

Compute this maximum while reading the sequence, using O(N)O(N)O(N) time and O(1)O(1)O(1) additional space.

Proof

Let qqq be the product of all values of kkk chosen so far, initially 111. The remaining elements always have original indices

q,2q,…,⌊Nq⌋q.q,2q,\ldots,\left\lfloor\frac{N}{q}\right\rfloor q.q,2q,…,
⌊qN​⌋
q
.

Keeping every kkk-th element leaves exactly the multiples of qkqkqk, preserving this property.

Exactly one element remains when ⌊N/q⌋=1\lfloor N/q\rfloor=1⌊N/q⌋=1, or equivalently,

⌊N2⌋+1≤q≤N.\left\lfloor\frac{N}{2}\right\rfloor+1\leq q\leq N.⌊2N​⌋+1≤q≤N.

Conversely, any index iii in this range can be left alone by choosing k=ik=ik=i in the first operation. If N=1N=1N=1, no operation is needed.

Thus, precisely the elements in the stated range can remain last.