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-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

MAXOR

Back to problem

Editorial

Let kkk be the integer satisfying 2k≤N<2k+12^k\leq N<2^{k+1}2k≤N<2k+1.

The numbers 1,2,4,…,2k1,2,4,\ldots,2^k1,2,4,…,2k are all available. Each of them contains exactly one distinct set bit, so we can choose them to set all of the lowest k+1k+1k+1 bits. This obtains

2k+1−1.2^{k+1}-1.2k+1−1.

Every integer from 111 to NNN is smaller than 2k+12^{k+1}2k+1. Therefore, XORing any selection cannot set a higher bit, and no result can exceed 2k+1−12^{k+1}-12.

Thus, the answer is one less than the smallest power of two greater than NNN. The time complexity is O(log⁡N)O(\log N)O(logN) per test case, and the additional space complexity is O(1)O(1)O(1).

Solution written by GPT6

k+1
−
1