Problem link: Problem - C - Codeforces
please somebody help me with my code for the problem C-Binary String Reconstruction. the code is running fine on the test cases but shoes wrong ans;
Approach- I firstly created a string ans of same size as string s with each element to be ‘a’. Then I itereate over string s and if s[i]==‘0’ then I put ans[i+x] =‘0’ (if i+x is within length of s) and ans[i-x] = ‘0’ (if i-x is within length of s).Then I iterate over s once again ,if s[i]==‘1’ then if ans[i+x]=‘0’ (if i+x is within length of s) and a[i-x]=‘0’ then i set flag as 1; at last iterate over string ans and replace all the remaining ‘a’ with ‘1’; At last if flag=1, print -1 else print ans;
my solution link: Submission #91052214 - Codeforces
solution:
#include<bits/stdc++.h>
#define endl ‘\n’
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define lli long long int
using namespace std;
int main(){
boost;
lli t;
cin>>t;
while(t- -)
{
lli flag=0;
string s;
cin>>s;
lli n;
cin>>n;
string ans(s.size(),‘a’);
for(lli i=0;i<s.size();i++)
{
if (s[i]=='0')
{
if (i+n<=s.size()-1)
{
ans[i+n]='0';
}
if (i-n>=0)
{
ans[i-n]='0';
}
}
}
for (lli i=0;i<s.size();i++ )
{
if (s[i]=='1')
{
if (i-n>=0 && i+n<=s.size()-1 && ans[i-n]=='0' && ans[i+n]=='0')
{
flag=1;
break;
}
else if (i-n<0 && i+n<=s.size()-1 && ans[i+n]=='0')
{
flag=1;
break;
}
else if (i-n>=0 && i+n>s.size()-1 && ans[i-n]=='0' )
{
flag=1;
break;
}
}
}
for (lli i=0;i<s.size();i++)
{
if (ans[i]=='a')
{
ans[i]='1';
}
}
if (flag==1)
{
cout<<-1<<endl;
}
else
{
cout<<ans<<endl;
}
}
}
please help me out.