Minimum adjacent swaps required to Sort Binary array (original) (raw)
Last Updated : 29 Apr, 2025
Given a binary array, the task is to find the **minimum number of **swaps needed to sort this binary array. We are allowed to swap only adjacent elements.
**Examples:
**Input : arr[] = [0, 0, 1, 0, 1, 0, 1, 1]
**Output : 3
**Explanation:
1st swap : [0, 0, 1, 0, 0, 1, 1, 1]
2nd swap : [0, 0, 0, 1, 0, 1, 1, 1]
3rd swap : [0, 0, 0, 0, 1, 1, 1, 1]**Input : arr[]= [0, 0, 0, 1, 1]
**Output : 0
**Explanation: Array is already sorted.
**Approach:
Since a sorted binary array would have all 0s followed by all 1s, we can process the array from right to left and calculate how many positions each 1 needs to travel through to reach its correct position. The count of positions for a 1 would be equal to number of zeros on right side of it.
Step by step approach:
- Start traversing the array from the end:
- If 0 is found, increment the zero count.
- When a 1 is found, it must move past all zeros to its right. So, add the count of zeros to the total swaps for each 1 encountered.
**Illustration:
Taking example of arr[] = [0, 0, 1, 0, 1, 0, 1, 1]:
- Position 7: Element is 1, zero count = 0, swaps = 0
- Position 6: Element is 1, zero count = 0, swaps = 0
- Position 5: Element is 0, zero count = 1, swaps = 0
- Position 4: Element is 1, zero count = 1, swaps = 1
- Position 3: Element is 0, zero count = 2, swaps = 1
- Position 2: Element is 1, zero count = 2, swaps = 3
- Position 1: Element is 0, zero count = 3, swaps = 3
- Position 0: Element is 0, zero count = 4, swaps = 3 + 0 = 3
C++ `
// C++ program to find Minimum adjacent // swaps required to Sort Binary array #include <bits/stdc++.h> using namespace std;
// Function to find Minimum adjacent // swaps required to Sort Binary array int minSwaps(vector &arr) { int n = arr.size(); int res = 0;
int zeroCount = 0;
for (int i = n - 1; i >= 0; i--) {
// If current element is 0,
// increment count of zeros
if (arr[i] == 0) {
zeroCount++;
}
// If current element is 1, it needs to be
// swapped with the number of zeros found
// so far.
else {
res += zeroCount;
}
}
return res;}
int main() { vector arr = {0, 0, 1, 0, 1, 0, 1, 1}; cout << minSwaps(arr); return 0; }
Java
// Java program to find Minimum adjacent // swaps required to Sort Binary array import java.util.*;
class GfG {
// Function to find Minimum adjacent
// swaps required to Sort Binary array
static int minSwaps(int[] arr) {
int n = arr.length;
int res = 0;
int zeroCount = 0;
for (int i = n - 1; i >= 0; i--) {
// If current element is 0,
// increment count of zeros
if (arr[i] == 0) {
zeroCount++;
}
// If current element is 1, it needs to be
// swapped with the number of zeros found
// so far.
else {
res += zeroCount;
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {0, 0, 1, 0, 1, 0, 1, 1};
System.out.println(minSwaps(arr));
}}
Python
Python program to find Minimum adjacent
swaps required to Sort Binary array
Function to find Minimum adjacent
swaps required to Sort Binary array
def minSwaps(arr): n = len(arr) res = 0
zeroCount = 0
for i in range(n - 1, -1, -1):
# If current element is 0,
# increment count of zeros
if arr[i] == 0:
zeroCount += 1
# If current element is 1, it needs to be
# swapped with the number of zeros found
# so far.
else:
res += zeroCount
return resif name == "main": arr = [0, 0, 1, 0, 1, 0, 1, 1] print(minSwaps(arr))
C#
// C# program to find Minimum adjacent // swaps required to Sort Binary array using System;
class GfG {
// Function to find Minimum adjacent
// swaps required to Sort Binary array
static int minSwaps(int[] arr) {
int n = arr.Length;
int res = 0;
int zeroCount = 0;
for (int i = n - 1; i >= 0; i--) {
// If current element is 0,
// increment count of zeros
if (arr[i] == 0) {
zeroCount++;
}
// If current element is 1, it needs to be
// swapped with the number of zeros found
// so far.
else {
res += zeroCount;
}
}
return res;
}
static void Main(string[] args) {
int[] arr = {0, 0, 1, 0, 1, 0, 1, 1};
Console.WriteLine(minSwaps(arr));
}}
JavaScript
// JavaScript program to find Minimum adjacent // swaps required to Sort Binary array
// Function to find Minimum adjacent // swaps required to Sort Binary array function minSwaps(arr) { let n = arr.length; let res = 0;
let zeroCount = 0;
for (let i = n - 1; i >= 0; i--) {
// If current element is 0,
// increment count of zeros
if (arr[i] == 0) {
zeroCount++;
}
// If current element is 1, it needs to be
// swapped with the number of zeros found
// so far.
else {
res += zeroCount;
}
}
return res;}
let arr = [0, 0, 1, 0, 1, 0, 1, 1]; console.log(minSwaps(arr));
`
**Time Complexity: O(n)
**Auxiliary Space: O(1)
Now try the below question yourself
What would be the count when we can swap any 0 with any 1?