Hi, My below code return a output and not passing the testcase even it satisfy the condition. Can somebody help me why it is failing? Problem link : https://cses.fi/problemset/task/2205/ For input : 3
My output: 000 100 110 010 101 001 011 111 CSES OUTPUT: 000 001 011 010 110 111 101 100 Code:
include <bits/stdc++.h>
define ll long long int
define f(i, p, q) for (int i = p; i < q; i++)
using namespace std; vectorv; void solve(int n,string s){ if(s.length()>=n){ v.push_back(s); return; } solve(n,s+'0'); solve(n,s+'1'); } int countOne(string s){ int count=0; for(int i=0;i<s.length();i++){ if(s[i]=='1')count++; } return count; } int main() { // For getting input from input.txt file // freopen("input.txt", "r", stdin);
// Printing the Output to output.txt file // freopen("output.txt", "w", stdout); int n; cin>>n; solve(n,""); map<int,vector<string>>mp; int ans=0; for(int i=0;i<v.size();i++){ mp[countOne(v[i])].push_back(v[i]); } int l=0; bool x= false; for(ll i=0;i<=pow(2,n);i++){ if(mp[l-1].size()>0 && x){ string g =mp[l-1][mp[l-1].size()-1]; cout<<g<<"\n"; mp[l-1].pop_back(); x=false; }else if(mp[l].size()>0 && !x){ string g =mp[l][mp[l].size()-1]; cout<<g<<"\n"; mp[l].pop_back(); x=true; } if(mp[l-1].size()==0){ l++; x= true; } }
}