Given a binary array, task is to sort this binary array using minimum swaps. We are allowed to swap only adjacent elements
Input : [0, 0, 1, 0, 1, 0, 1, 1] Output : 3 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 : Array = [0, 1, 0, 1, 0] Output : 3
This can be done by finding number of zeroes to the right side of every 1 and add them. In order to sort the array every one always has to perform a swap operation with every zero on its right side. So the total number of swap operations for a particular 1 in array is the number of zeroes on its right hand side. Find the number of zeroes on right side for every one i.e. the number of swaps and add them all to obtain the total number of swaps.
Time Complexity : O(n)
Auxiliary Space : O(n)
Space Optimized Solution :
An auxiliary space is not needed. We just need to start reading the list from the back and keep track of number of zeros we encounter. If we encounter a 1 the number of zeros is the number of swaps needed to put the 1 in correct place.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum number of swaps required for arranging pairs adjacent to each other
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum number of swaps required to sort an array | Set 2
- Minimum number of swaps required to sort an array of first N number
- Queries to find minimum swaps required to sort given array with updates
- Minimum number of swaps required to sort an array
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Number of swaps to sort when only adjacent swapping allowed
- Minimum swaps required to convert one binary string to another
- Minimum swaps required to make a binary string alternating
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum number of adjacent swaps for arranging similar elements together
- Minimum Swaps required to group all 1's together
- Minimum swaps required to bring all elements less than or equal to k together
- Minimum swaps of same indexed elements required to obtain a Majority Element in one of the arrays
- Maximum number formed from array with K number of adjacent swaps allowed
- Convert one array to another using adjacent swaps of elements
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.