The "Minimum Toggle to Partition" problem is a binary array problem where we are given an array containing only 0 and 1.
The goal is to make the array partitioned in this format:
0 0 0 0 ... 1 1 1 1
This means:
- All
0s should appear first - All
1s should appear after them
We are allowed to toggle elements:
0 → 11 → 0
The task is to find the minimum number of toggles required to make the binary array properly partitioned.
This is a classic greedy and prefix-counting style DSA problem often asked in coding interviews and competitive programming contests.
| Constraint | Value |
|---|---|
| Array Size | 1 ≤ arr.size() ≤ 10^5 |
| Element Value | 0 ≤ arr[i] ≤ 1 |
When I first saw this problem, I realized that the final array must have exactly one transition point.
Everything before that point should be 0, and everything after that point should be 1.
So instead of directly modifying the array, I started thinking about every possible partition position.
For every partition:
- Left side should contain only
0 - Right side should contain only
1
That means:
- Every
1on the left side is wrong - Every
0on the right side is wrong
If I can count both efficiently, then I can compute the number of required toggles for every partition and take the minimum.
That observation leads directly to an efficient linear-time solution.
First, I count the total number of zeros in the array.
Why?
Because initially I assume the entire array belongs to the right side, where everything should be 1. So every 0 currently present would need a toggle.
Then I traverse the array from left to right.
During traversal:
- If I see a
0, I remove it from the right-side zero count - If I see a
1, I add it to the left-side one count
At every step:
leftOnes= invalid elements on left siderightZeros= invalid elements on right side
So total toggles become:
leftOnes + rightZeros
I keep updating the minimum answer while traversing the array once.
This gives an optimal O(n) solution.
| Data Structure | Purpose |
|---|---|
| Integer Variables | Used for counting left-side 1s, right-side 0s, and storing the minimum answer |
| Array | Input binary array traversal |
No extra arrays, stacks, queues, maps, or sets are needed.
-
Count total zeros in the array
-
Assume entire array is initially on the right side
-
Start traversing from left to right
-
Move elements one by one from right partition to left partition
-
Track:
- how many
1s are on the left - how many
0s are on the right
- how many
-
Compute toggles needed for current partition
-
Update the minimum answer
-
Return the smallest toggle count found
| Type | Complexity | Explanation |
|---|---|---|
| Time Complexity | O(n) |
The array is traversed only once |
| Space Complexity | O(1) |
Only a few integer variables are used |
Where:
n= size of the binary array
class Solution {
public:
int minToggle(vector<int>& arr) {
// Count total zeros in the array
int rightZeros = 0;
for (int num : arr) {
if (num == 0)
rightZeros++;
}
// Minimum answer when entire array is considered right side
int ans = rightZeros;
// Count of ones on the left side
int leftOnes = 0;
// Traverse the array and try every partition
for (int num : arr) {
// Current zero moves from right side to left side
if (num == 0)
rightZeros--;
// Current one becomes part of left side
else
leftOnes++;
// Total toggles needed for current partition
ans = min(ans, leftOnes + rightZeros);
}
return ans;
}
};class Solution {
int minToggle(int[] arr) {
// Count total zeros in the array
int rightZeros = 0;
for (int num : arr) {
if (num == 0)
rightZeros++;
}
// Initial answer
int ans = rightZeros;
// Count ones on left side
int leftOnes = 0;
// Try every partition
for (int num : arr) {
// Remove zero from right side
if (num == 0)
rightZeros--;
// Add one to left side
else
leftOnes++;
// Update minimum toggles
ans = Math.min(ans, leftOnes + rightZeros);
}
return ans;
}
}class Solution {
minToggle(arr) {
// Count total zeros
let rightZeros = 0;
for (let num of arr) {
if (num === 0)
rightZeros++;
}
// Initial minimum answer
let ans = rightZeros;
// Count ones on left side
let leftOnes = 0;
// Traverse array
for (let num of arr) {
// Current zero leaves right side
if (num === 0)
rightZeros--;
// Current one enters left side
else
leftOnes++;
// Update answer
ans = Math.min(ans, leftOnes + rightZeros);
}
return ans;
}
}class Solution:
def minToggle(self, arr):
# Count total zeros in array
rightZeros = 0
for num in arr:
if num == 0:
rightZeros += 1
# Initial answer
ans = rightZeros
# Count ones on left side
leftOnes = 0
# Traverse array
for num in arr:
# Remove zero from right side
if num == 0:
rightZeros -= 1
# Add one to left side
else:
leftOnes += 1
# Update minimum answer
ans = min(ans, leftOnes + rightZeros)
return ansThe logic remains exactly the same in all languages. Only syntax changes.
First, we count the total number of zeros in the array.
This count represents how many wrong elements currently exist on the right side if we place the partition before the first element.
Then we create another variable to track how many 1s appear on the left side while traversing.
As we move through the array:
- A
0leaves the right side - A
1enters the left side
At every step:
- Left-side
1s are invalid because left side should contain only0 - Right-side
0s are invalid because right side should contain only1
So the total toggles required become:
leftOnes + rightZeros
We continuously update the minimum answer.
This approach works efficiently because:
- We never recompute counts from scratch
- Every element is processed only once
- No extra memory is required
The final valid array always has exactly one partition point.
So instead of trying all possible toggle combinations, we only try all possible partition positions.
That reduces the problem from exponential possibilities to a simple linear scan.
Input:
[0, 0, 0]
Already partitioned.
Answer:
0
Input:
[1, 1, 1]
Already partitioned.
Answer:
0
Input:
[1, 0, 1, 0, 1]
Multiple invalid positions exist, so toggles are required.
The algorithm checks every partition efficiently.
Input:
arr = [1, 0, 1, 1, 0]
Output:
2
Explanation:
One valid final array can be:
[0, 0, 1, 1, 1]
Changes made:
- First
1 → 0 - Last
0 → 1
Total toggles = 2
Input:
arr = [0, 1, 0, 0, 1, 1, 1]
Output:
1
Explanation:
Only one toggle is needed:
[0, 0, 0, 0, 1, 1, 1]
The second element changes from 1 → 0.
Input:
arr = [1, 1, 1, 1]
Output:
0
Explanation:
The array is already partitioned because all 1s are together after zero 0s.
Compile:
g++ solution.cpp -o solutionRun:
./solutionCompile:
javac Solution.javaRun:
java SolutionRun using Node.js:
node solution.jsRun:
python solution.py-
This problem is best solved using greedy counting with prefix-style tracking.
-
A brute-force solution would try every partition and recount values every time, leading to
O(n²)complexity. -
The optimized solution avoids repeated counting.
-
No additional memory allocation is required.
-
This approach is interview-friendly because it demonstrates:
- observation skills
- greedy thinking
- efficient traversal
- space optimization
Another approach is using prefix and suffix arrays:
- Prefix array for counting left-side
1s - Suffix array for counting right-side
0s`
But that requires extra memory.
The current approach achieves the same result using constant space.