Given a binary string S of length N and an integer K, the task is to find the minimum number of adjacent swaps required to make the binary string divisible by 2K. If it is not possible then print -1.
Input: S = “100111”, K = 2
Swapping the right-most zero 3 times
to the right, we get “101110”.
Swapping the second right-most zero
3 times to the right, we get “111100”.
Input: S = “1011”, K = 2
Method 1: An approach will be swapping from the right-most zero. So, let’s rephrase the problem to something more simpler. The minimum number of swaps are required such that at least K consecutive zeros are made available at the right end.
One way will be to simulate the swapping. Starting from the right-most zero, swap it till it has 1 at its right and its not the end of the string. This will be performed for the K rightmost zeros. The time complexity of this approach will be O(N * K).
Method 2: The key for performing better here will be to avoid doing the simulation.
Among the K right-most zeros, each zero will need to be swapped X number of times, where X is the number of 1s to the right of that zero.
Thus, for the K right-most zeros, the task is to find the sum of the number of 1s to the right of each of them.
- Initialise variable c_zero = 0, c_one = 0 and ans = 0.
- Run a loop from i = N – 1 to i = 0.
- If S[i] = 0 then update c_zero = c_zero + 1 and ans = ans + c_one.
- If S[i] = 1 then update c_one = c_one + 1.
- If c_zero = K then break.
- If c_zero < K then return -1.
- Finally, return the ans.
Thus, the time complexity of this approach will be O(N).
Below is the implementation of the above approach:
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 swaps required to make a binary string alternating
- Minimum number of swaps required to make the string K periodic
- Minimum number of swaps required to make a number divisible by 60
- Minimum swaps required to convert one binary string to another
- Minimum number of swaps to make two binary string equal
- Minimum adjacent swaps required to Sort Binary array
- Minimum number of given moves required to make N divisible by 25
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Minimum operations required to make all Array elements divisible by K
- Minimum swaps to make two strings equal by swapping only with third string
- Minimum flip required to make Binary Matrix symmetric
- Minimum changes required to make first string substring of second string
- Minimum operations required to make the string satisfy the given condition
- Minimum flips required to keep all 1s together in a Binary string
- Minimum number of operations required to sum to binary string S
- Minimum given operations required to convert a given binary string to all 1's
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : AnkitRai01