SWAP10HG - Editorial

take this as testcase
1
2
01
10

here you need to understand you canot swap 0 to 1 because you should choose two indices ii and jj (1≤i<j≤N1≤i<j≤N) such that Si is ‘1’ and Sj is ‘0’, and swap Si with Sj. hope this will help you

your code will give you “YES” but correct answer is “NO”

My Bruteforce solution got 100 points.
Solution-Click here

Were test cases weak for this problem?

Solution: 40865018 | CodeChef
why it is getting wa?

1 Like

Your code will fail on this test case:

1
3
011
101

Expected output : No
Your Output : Yes

You can’t swap a ‘0’ with ‘1’ if 0 is present behind 1.
While
You can swap a ‘0’ with ‘1’ if 0 is present after 1.

2 Likes

thanks I got it

1 Like

Hey Everybody,
Link for my solution
Solution: 40830211 | CodeChef
Can anybody help me get why this solution is wrong because this solution got 20 pts initially but after CodeChef removed some test cases the same solution got 70 pts but I am not able to get how this solution is getting WA is some cases?

I got WA. Could you please help me in this :
https://www.codechef.com/viewsolution/40895063

Hi, you are only counting number of 1’s and 0’s in S and P, and comparing them. But the question is “Can we convert S into P”, but there is a condition un that conversion also.

You have to convert the string SS into PP using zero or more operations. In one operation, you should choose two indices ii and jj (1≤i<j≤N1≤i<j≤N) such that SiSi is ‘1’ and SjSj is ‘0’, and swap SiSi with SjSj

1 Like

https://www.codechef.com/viewsolution/40876472
Can someone give test cases where my code fails?

Would anyone be able to find the error in the following code? It is failing on two tests
submission - Solution: 40901272 | CodeChef

LOGIC

  1. Find the indices of the misplaced 0s in the s string
  2. Iterate once over s and if s[i]==p[i] continue
  3. Else, if s[i] ==‘1’ try to find an index greater than i with misplaced zero, swap elements and remove that index from list
  4. And If s[i]==‘0’ then we can’t ever change it to 1 as all ones before it must be placed correctly due to step 3, so just break.

why 1st test case is WA what is wrong in this code Solution: 40928591 | CodeChef can any one check it

my solution

I think it’s right, but it shows wrong for all test cases, please help me out here.

hello
bool good = count(all(s), ‘1’) == count(all(t), ‘1’);
someone please tell me functionality of all() and what is it that is getting stored in good variable?


//Please tell me in which case my code fails

#include <bits/stdc++.h>
using namespace std;
signed main()
{
	int t;	cin>>t;
	while(t--){
		int n, i, p0=0, p1=0, c0=0, c1=0;
		cin>>n;
		string s, p;
		cin>>s>>p;
		
		for(i=0;i<n;i++){
			if(s[i]=='1') c1++;
			
			if(p[i]=='1') p1++;
		}
		if(c1==p1 && s>=p){
			cout<<"Yes"<<endl;
		}
		else
			cout<<"No"<<endl;
	}
}