← All problemsSign in

Minimise Inversions

CodeChefRating 2263Open on judge ↗

You are given a **binary** string $S$ of length $N \ (1 \le N \le 5 \cdot 10^3)$ and an integer $K$. You can flip$^\dag$ **at most** $K$ characters of the string $S$. Find the **minimum** number of inversions$^\ddag$ in the final string. $^\dag$During a flip, $0$ changes to $1$ and vice-versa. $^\ddag$A pair of indices $(i,j)$ is said to be an inversion if $1 \le i\lt j \le |S|$ and $S_i\gt S_

HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17

CodeSearch Tutor

Hints, not spoilers — it won’t hand over the full solution unless you insist.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start