Flip to Invert Codechef Solution | APRIL CHALLENGE


Flip to Invert Codechef Solution

JJ has a binary string SS of length NN. JJ can perform the following operation on SS:

  • Select an ii such that 1≤i≤N1≤i≤N, and flip SiSi (i.e. change 00 to 11 and 11 to 00)

JJ wants to minimize the number of inversions in SS by performing the above operation at most KK times. Can you help JJ do so?

Recall that a pair of indices (i,j)(i,j) in SS is called an inversion if i<ji<j and Si>SjSi>Sj.

Input Format

  • The first line contains a single integer TT – the number of test cases. Then the test cases follow.
  • The first line of each test case contains two integers NN and KK – the length of the binary string SS and the maximum number of times JJ can perform the given operation.
  • The second line of each test case contains a binary string SS of length NN containing 00s and 11s only.

Output Format

For each test case, output the minimum number of inversions possible after performing the given operation at most KK times.


  • 1≤T≤1051≤T≤105
  • 1≤N≤1051≤N≤105
  • 0≤K≤N0≤K≤N
  • Sum of NN over all test cases does not exceed 2⋅1052⋅105

Sample Input 1 

8 3
5 1
6 2

Sample Output 1 



Test case 1: We are allowed to perform at most 33 operations. We can perform the following operation on SS: 00010–111→0001111100010_111→00011111 which has 00 inversions. Therefore 00 is the answer.

Test case 2: We can perform the following operation on SS: 1–0100→001001_0100→00100 which has 22 inversions. It can be proven that this is the minimum number of inversions possible.

Test case 3: We can perform the following operations on SS: 11100–0→111010–→11101111100_0→111010_→111011 which has 33 inversions. It can be proven that this is the minimum number of inversions possible.

Flip to Invert Codechef  SOLUTION

Download Code
login to google play console, youtube video search by image, how to add a google account, which are the google apps, what is the google app, how to add a new google account, all the google apps, which service is the part of google, adsetting google, google data studio, google DNS, GCP console, bigquery, google studio, google api console, google app engine, anthos, google apis, google public DNS, google audio, google launcher, gcloud console, google compute engine, google webmaster console, google logged-in devices, gdrive unlimited,google console tools,google studio login,my google voice,hey google service,find similar images google, my google forms, www google com bd search, search using images in google,google image search scan,google sea console,website console google, google sound, gdrive storage for students, google site console, google console login, webmaster console, searc h console, google server down, google backup login, photo finder google, google products and services, google console tool, google image check, google cloud search is used for, google dashboard studio, google voice and audio activity,gcp dashboards, google services down, google go app, google dns down, google image identifier,check picture on google,google new launcher,google image finder online,image check google google google go, image finder on google, finding images on google, photo find google, find a picture in google, find the photo on google, picture find google, image find on google, find with photo google, image finder google online, google find with image, photo finder in google, google find image search, search using image in google, search on google with image, google server down today, google server down news

Read More Post Here

Leave a Comment

Your email address will not be published. Required fields are marked *