@mohmum your code fails for 1 1 1 answer should be zero but your code prints 1
can someone…anyone tell me whats wrong in my code…It run perfectly in devC and codeblocks
#include
using namespace std;
int main() {
int t;
cin>>t;
while(t–)
{
int n,arr[1000],ct=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=0;i<n;i++)
{
if(i==0 && arr[i+1]==0 && arr[i]==0)
{
ct++;
}
else if(i==n-1 && arr[i-1]==0 && arr[i]==0)
{
ct++;
}
else if( i>0 && arr[i]==0 && arr[i+1]==0 && arr[i-1]==0)
{
ct++;
}
}
if(ct>0)
{
cout<<ct<<endl;
}
else
cout<<0<<endl;
}
// your code goes here
return 0;
}
Please either format your code or link to your submission - the forum software has mangled it and it won’t compile!
you’re so good…thank you so much for replying!!!
Here’s the link of my code…
https://www.codechef.com/viewsolution/28553084
I know
Thanks!
Consider the testcase:
1
6
000110
(the answer should be 2
).
the output is 2:frowning_face:
Based on your screenshot, you’ve added spaces between all the 0’s and 1’s in the testcase I gave you, for some reason.
Oopsi desy…Got it…
I’ll correct my mistake and let you know the output…
Thanks a lot !!!
it run successfully now…without any memory,time limit,or run time errors…PhewwWwww!!!
Thank You !!!
how did your code work for n = 2 cases? wouldn’t count be incremented twice?
Example?
It goes in the bottom part only when n is 1. Otherwise it just does whatever your code does.
say i enter 00, wouldn’t count become 2?
Yes?, it’s supposed to be 2.
forget it . My bad
fixed it and it worked. thanks man
Whats wrong in my code?
for _ in range (int(input())):
n=int(input())
s=input()
s=“a”+s+“a”
s=list(s)
for i in range (1,n+1):
if(s[i]==“1”):
s[i]=“a”
s[i-1]=“a”
s[i+1]=“a”
print(s.count(“0”))
can any one tell what is wrong with my code it is showing wrong answer
ll t;
cin>>t;
while(t–)
{
ll n,i,ans=0;
cin>>n;
bool res[n];
int arr[n];
for(i=0;i<n;i++)
{
cin>>arr[i];
res[i]=false;
}
if(n==1)
{
if(arr[i]==1)
{
cout<<0<<endl;
continue;
}
else
{
cout<<1<<endl;
continue;
}
}
for(i=0;i<n;i++)
{
if(arr[i]==1)
{
if(i==0)
{
res[0]=res[1]=true;
}
else if(i==n-1)
{
res[n-1]=res[n-2]=true;
}
else
{
res[i]=res[i-1]=res[i+1]=true;
}
}
}
for(i=0;i<n;i++)
{
if(res[i]==false)
ans++;
}
cout<<ans<<endl;
}