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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum number of swaps required to sort an array | Set 2
- Minimum number of swaps required to sort an array
- Minimum number of swaps required for arranging pairs adjacent to each other
- Minimum number of swaps required to sort an array of first N number
- Minimum swaps required to convert one binary string to another
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum swaps required to make a binary string alternating
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Number of swaps to sort when only adjacent swapping allowed
- Minimum increment or decrement required to sort the array | Top-down Approach
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Minimum Swaps required to group all 1's together
- Minimum number of adjacent swaps for arranging similar elements together
- Minimum swaps required to bring all elements less than or equal to k together
- Minimum time required to cover a Binary Array
- Convert one array to another using adjacent swaps of elements
- Minimum inversions required so that no two adjacent elements are same
- Minimum swaps so that binary search can be applied
- Minimum number of swaps to make two binary string equal
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.