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 951/D. Fixing a Binary String
Prev
CodeforcesCodeforces
Expert
Codeforces Round 951

D. Fixing a Binary String

GreedyStringBitmaskNumber Theory
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

You are given a binary string π‘  of length π‘›, consisting of zeros and ones. You can perform the following operation exactly once:

  1. Choose an integer π‘ (1≀𝑝≀𝑛).
  2. Reverse the substring π‘ 1𝑠2…𝑠𝑝. After this step, the string π‘ 1𝑠2…𝑠𝑛 will become π‘ π‘π‘ π‘βˆ’1…𝑠1𝑠𝑝+1𝑠𝑝+2…𝑠𝑛.
  3. Then, perform a cyclic shift of the string π‘  to the left π‘ times. After this step, the initial string π‘ 1𝑠2…𝑠𝑛 will become π‘ π‘+1𝑠𝑝+2β€¦π‘ π‘›π‘ π‘π‘ π‘βˆ’1…𝑠1.

For example, if you apply the operation to the string 110001100110 with π‘=3, after the second step, the string will become 011001100110, and after the third step, it will become 001100110011.

A string π‘  is called π‘˜-proper if two conditions are met:

  • 𝑠1=𝑠2=…=π‘ π‘˜;
  • 𝑠𝑖+π‘˜β‰ π‘ π‘– for any π‘– (1β‰€π‘–β‰€π‘›βˆ’π‘˜).

For example, with π‘˜=3, the strings 000, 111000111, and 111000 are π‘˜-proper, while the strings 000000, 001100, and 1110000 are not.

You are given an integer π‘˜, which is a divisor of π‘›. Find an integer π‘ (1≀𝑝≀𝑛) such that after performing the operation, the string π‘  becomes π‘˜-proper, or determine that it is impossible. Note that if the string is initially π‘˜-proper, you still need to apply exactly one operation to it.

Solutions & Walkthrough

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

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