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;
}
getting this C++ forbids comparison between pointer and integer
my code
#include
#include
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
string s;
cin>>s;
int c=0;
for(int i=0 ; i<n; i++){
if(i==0){
if(s[i]==“0” && s[i+1]==“0”){
c++;
}
}
else if(i==n-1){
if(s[i]==“0” && s[i-1]==“0”){
c++;
}
}
else{
if(s[i]==“0” && s[i+1]==“0” && s[i-1]==“0”){
c++;
}
}
}
cout<<c<<"\n";
}
return 0;
}
please help
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile!
Edit:
I can probably decipher enough of this to make a guess: replace all occurrences of
"0"
with
'0'
(the former is of type const char*
; the latter is char
. The type of s[someInt]
is char
, hence the error).
Edit2:
clang
seems to be more helpful, here:
[simon@simon-laptop][18:22:39]
[~/devel/hackerrank/otherpeoples]>clang++ -std=c++14 bhavitjain11-LEBOMBS.cpp -O3 -g3 -Wall -Wextra
bhavitjain11-LEBOMBS.cpp:17:28: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if(s[i]=="0" && s[i+1]=="0"){
^ ~~~
bhavitjain11-LEBOMBS.cpp:17:28: error: comparison between pointer and integer ('int' and 'const char *')
if(s[i]=="0" && s[i+1]=="0"){
~~~~^ ~~~
bhavitjain11-LEBOMBS.cpp:17:43: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if(s[i]=="0" && s[i+1]=="0"){
^ ~~~
bhavitjain11-LEBOMBS.cpp:17:43: error: comparison between pointer and integer ('int' and 'const char *')
if(s[i]=="0" && s[i+1]=="0"){
~~~~~~^ ~~~
bhavitjain11-LEBOMBS.cpp:22:28: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if(s[i]=="0" && s[i-1]=="0"){
^ ~~~
bhavitjain11-LEBOMBS.cpp:22:28: error: comparison between pointer and integer ('int' and 'const char *')
if(s[i]=="0" && s[i-1]=="0"){
~~~~^ ~~~
bhavitjain11-LEBOMBS.cpp:22:43: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if(s[i]=="0" && s[i-1]=="0"){
^ ~~~
bhavitjain11-LEBOMBS.cpp:22:43: error: comparison between pointer and integer ('int' and 'const char *')
if(s[i]=="0" && s[i-1]=="0"){
~~~~~~^ ~~~
bhavitjain11-LEBOMBS.cpp:27:28: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if(s[i]=="0" && s[i+1]=="0" && s[i-1]=="0"){
^ ~~~
bhavitjain11-LEBOMBS.cpp:27:28: error: comparison between pointer and integer ('int' and 'const char *')
if(s[i]=="0" && s[i+1]=="0" && s[i-1]=="0"){
~~~~^ ~~~
bhavitjain11-LEBOMBS.cpp:27:43: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if(s[i]=="0" && s[i+1]=="0" && s[i-1]=="0"){
^ ~~~
bhavitjain11-LEBOMBS.cpp:27:43: error: comparison between pointer and integer ('int' and 'const char *')
if(s[i]=="0" && s[i+1]=="0" && s[i-1]=="0"){
~~~~~~^ ~~~
bhavitjain11-LEBOMBS.cpp:27:58: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if(s[i]=="0" && s[i+1]=="0" && s[i-1]=="0"){
^ ~~~
bhavitjain11-LEBOMBS.cpp:27:58: error: comparison between pointer and integer ('int' and 'const char *')
if(s[i]=="0" && s[i+1]=="0" && s[i-1]=="0"){
~~~~~~^ ~~~
bhavitjain11-LEBOMBS.cpp:35:20: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if(s[0]=="0"){
^ ~~~
bhavitjain11-LEBOMBS.cpp:35:20: error: comparison between pointer and integer ('int' and 'const char *')
if(s[0]=="0"){
~~~~^ ~~~
8 warnings and 8 errors generated.
Thanks - see my Edit
thanks sir it works
thanks
Can you tell me what is wrong in this code?
#include
#include<bits/stdc++.h>
#include
using namespace std;
#define ll long long int
int main(){
int t;
cin>>t;
int n;
string s;
while(t–){
cin>>n;
cin>>s;
for(int i=0;i<n;i++){
if(s[i]=='1' ){
s[i]='x';
if(i!=0) s[i-1]='x';
if(i!=n-1) s[i+1]='x';
}
}
int cnt =0;
for(int i=0;i<n;i++){
if(s[i]==‘0’){
cnt++;
}
}
cout<<cnt<<endl;
}
}
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile!
Edit:
Meh - decipherable enough, I guess: fails on same testcase as this guy.