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:
- Minimum swaps required to make a binary string alternating
- 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 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 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 number of replacements to make the binary string alternating | Set 2
- Minimum number of characters to be removed to make a binary string alternate
- Minimum number of changes required to make the given array an AP
- Minimum changes required to make all element in an array 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 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