Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew πŸš€
Contest SolutionsPractice
Practice Sheets
MasterclassesLive πŸ‘¨πŸ»β€πŸ’»

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
Β© 2026coding75β€’crackDSAβ„’β€’Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021β€’Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/Codeforces/Codeforces Round 947/C. Chamo and Mocha's Array
Prev
CodeforcesCodeforces
Specialist
Codeforces Round 947

C. Chamo and Mocha's Array

GreedyArrayMath
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

Mocha likes arrays, so before her departure, Chamo gave her an array π‘Ž consisting of π‘› positive integers as a gift.

Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times:

  1. Choose indices l and π‘Ÿ (1≀𝑙<π‘Ÿβ‰€π‘›)
  2. Let π‘₯ be the median of the subarray [π‘Žπ‘™,π‘Žπ‘™+1,…,π‘Žπ‘Ÿ]
  3. Set all values π‘Žπ‘™,π‘Žπ‘™+1,…,π‘Žπ‘Ÿ to π‘₯

Suppose π‘Ž=[1,2,3,4,5] initially:

  • If Mocha chooses (𝑙,π‘Ÿ)=(3,4) in the first operation, then π‘₯=3, the array will be changed into π‘Ž=[1,2,3,3,5].
  • If Mocha chooses (𝑙,π‘Ÿ)=(1,3) in the first operation, then π‘₯=2, the array will be changed into π‘Ž=[2,2,2,4,5].

Mocha will perform the operation until the array contains only the same number. Mocha wants to know what is the maximum possible value of this number.† The median in an array b of length π‘š is an element that occupies position number βŒŠπ‘š+12βŒ‹ after we sort the elements in non-decreasing order. For example, the median of [3,1,4,1,5] is 3 and the median of [5,25,20,24] is 20.

Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Codeforces Round 947
Previous Problem