How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Difficulty: ๐ด Hard
Topics: โข Greedy โข Parity
You are given:
skIn one operation, you must:
k different indices'0' โ '1''1' โ '0'Return the minimum number of operations required to make all characters equal to '1'.
If it is not possible, return:
-1
s = "110"
k = 1
1
There is one '0' in the string.
Since k = 1, we can flip it directly in one operation.
s = "0101"
k = 3
2
One optimal sequence:
Operation 1:
Flip indices [0, 1, 3]
"0101" โ "1000"
Operation 2:
Flip indices [1, 2, 3]
"1000" โ "1111"
โ Minimum operations = 2
s = "101"
k = 2
-1
There is only one '0', but each operation must flip exactly 2 indices.
โ ๏ธ Impossible to make all characters '1'.
1 <= s.length <= 10^5s[i] is either '0' or '1'1 <= k <= s.lengthEach operation flips exactly k bits.
Think in terms of:
public class Main {
public static int minimumOperations(String s, int k) {
int n = s.length();
int zeroCount = 0;
// Count number of zeros
for (int i = 0; i < n; i++) {
if (s.charAt(i) == '0') {
zeroCount++;
}
}
// If already all 1s
if (zeroCount == 0) {
return 0;
}
// Try possible number of operations
for (int t = 1; t <= n; t++) {
int totalFlips = t * k;
// Condition 1: must flip at least all zeros
if (totalFlips < zeroCount) continue;
// Condition 2: extra flips must be even
if ((totalFlips - zeroCount) % 2 == 0) {
return t;
}
}
return -1;
}
public static void main(String[] args) {
// Hardcoded test cases
String s1 = "110";
int k1 = 1;
String s2 = "0101";
int k2 = 3;
String s3 = "101";
int k3 = 2;
int ans1 = minimumOperations(s1, k1);
int ans2 = minimumOperations(s1, k1);
int ans3 = minimumOperations(s1, k1);
System.out.println("Example 1 Output: " + ans1);
System.out.println("Example 2 Output: " + ans2);
System.out.println("Example 3 Output: " + ans3);
}
}
๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.