Binary Minimal

can anyone tell what is wrong in this code why its not get accepted for this contest problem: Binary Minimal Practice Coding Problem - CodeChef

// Jai Bajrangbali
include <bits/stdc++.h>
typedef long long ll;
using namespace std;
void sol();
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t = 1;
cin >> t;
while (t–)
{
sol();
}
return 0;
}

void sol()
{
ll n, k;
cin >> n >> k;
string s;
cin >> s;
ll cnt = 0, ans = 0, mx = 0;
for (ll i = 0; i < n; i++)
{
if (s[i] == ‘1’ && k > 0)
{
s[i] = ‘0’;
k–;
}
}
for (ll i = 0; i < n; i++)
{
if (s[i] == ‘1’)
{
cout << s << ‘\n’;
return;
}
}
for (ll i = 0; i < n - k; i++)
cout << “0”;
cout << “\n”;
}

There is issue with your understanding of the problem

Answer for s = 10 and k = 1 isn’t 00. It’s 0.

So there will be 3 conditions for the problem

1. When the number of ones < k
→ For this case, your approach is the right one. Just replace all 1s with 0s because there is no other way to get lexicographically smaller string.

2. Number of ones greater == k
→ For this case, answer will be all zeros

3. Number of ones > k
→ For this case, remove all ones. Then find remaining k as k=k-ones. Then remove k zeros.

Example
s=11001 k=4

ones = 3
k >= ones

So first remove all ones. SO now s=00

Now k = 4-ones = 4-3 = 1

So now remove 1 0

So final answer is 0