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

Flattening a Sequence

Back to problem

Editorial

The final common value ttt cannot exceed min⁡A\min AminA, so the maximum element needs at least max⁡A−t≥max⁡A−min⁡A\max A-t\geq\max A-\min AmaxA−t≥maxA−minA decrements. Decreasing all elements above min⁡A\min AminA each time attains this bound. The answer is the range, in O(N)O(N)O(N) time and O(1)O(1)O(1) auxiliary space. Sorting is a naive O(Nlog⁡N)O(N\log N)O(NlogN) solution that also passes. The answer is at most 2⋅1092\cdot10^92⋅109, within signed 32-bit; the implementations use 64-bit.

Solution written by GPT6