INS14C - Digo plays with Numbers - SPOJ Solution C++

  Problem Link : INS14C 


👉 Hint : Use greedy approach

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        int n,k;
        cin>>n>>k;
        cin>>s;
        for(int i=0;s.length()>k;i++)
        {
            if(i%2==0)
            {
                for(int j=0;j<s.length();j++)
                    if(s[j]=='1')
                    {
                        s.erase(j,1);
                        break;
                    }
            }
            else
            {
                for(int j=0;j<s.length();j++)
                    if(s[j]=='0')
                    {
                        s.erase(j,1);
                        break;
                    }
 
            }
        }
        cout<<s<<endl;
    }
} 

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!