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/Educational Codeforces Round 165/B. Shifts and Sorting
PrevNext
CodeforcesCodeforces
Pupil
Educational Codeforces Round 165

B. Shifts and Sorting

GreedyBit ManipulationStringArray
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

Let's define a cyclic shift of some string π‘  as a transformation from π‘ 1𝑠2β€¦π‘ π‘›βˆ’1𝑠𝑛 into π‘ π‘›π‘ 1𝑠2β€¦π‘ π‘›βˆ’1. In other words, you take one last character π‘ π‘› and place it before the first character while moving all other characters to the right.

You are given a binary string π‘  (a string consisting of only 0-s and/or 1-s).

In one operation, you can choose any substring π‘ π‘™π‘ π‘™+1β€¦π‘ π‘Ÿ (1≀𝑙<π‘Ÿβ‰€|𝑠|) and cyclically shift it. The cost of such operation is equal to π‘Ÿβˆ’𝑙+1 (or the length of the chosen substring).

You can perform the given operation any number of times. What is the minimum total cost to make π‘  sorted in non-descending order?

Solutions & Walkthrough

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

Connecting secure classroom stream...
Back to Educational Codeforces Round 165
Previous ProblemNext Problem