Flip to Invert Solution CodeChef | CodeChef Problem Solution 2022
JJ has a binary string of length . JJ can perform the following operation on :
- Select an such that , and flip (i.e. change to and to )
JJ wants to minimize the number of inversions in by performing the above operation at most times. Can you help JJ do so?
Recall that a pair of indices in is called an inversion if and .
Input Format
- The first line contains a single integer - the number of test cases. Then the test cases follow.
- The first line of each test case contains two integers and - the length of the binary string and the maximum number of times JJ can perform the given operation.
- The second line of each test case contains a binary string of length containing s and s only.
Solution Click Below:-
Output Format
For each test case, output the minimum number of inversions possible after performing the given operation at most times.
Constraints
- Sum of over all test cases does not exceed
Sample Input 1
3
8 3
00010111
5 1
10100
6 2
111000
Sample Output 1
0
2
3
Explanation
Test case 1: We are allowed to perform at most operations. We can perform the following operation on : which has inversions. Therefore is the answer.
Test case 2: We can perform the following operation on : which has inversions. It can be proven that this is the minimum number of inversions possible.
Test case 3: We can perform the following operations on : which has inversions. It can be proven that this is the minimum number of inversions possible.
No comments:
Post a Comment