Gray Code cses problem: Why my output is not passing the testcase.

Revision en2, by Thanos_I_am_inevitable, 2024-01-06 16:13:30

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;

    }
}

}

Tags adhoc

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English Thanos_I_am_inevitable 2024-01-06 16:24:06 227
en4 English Thanos_I_am_inevitable 2024-01-06 16:21:54 20
en3 English Thanos_I_am_inevitable 2024-01-06 16:20:30 4 Tiny change: 'ace std;\n\n\nvector<s' -> 'ace std;\nvector<s'
en2 English Thanos_I_am_inevitable 2024-01-06 16:13:30 2 Tiny change: 'ode:\n\n\n#inc' -> 'ode:\n\n\n\n#inc'
en1 English Thanos_I_am_inevitable 2024-01-06 16:12:45 1764 Initial revision (published)