i Just wanna ask that in this question can the ans be odd !?
or ans needs to be even necessarily!?
Yes the answer can be odd and for the test case below the answer will be 5
Input:
1
4
01010110
Output:
5
Explanation: As after the 5th index ( 3rd time A shoot) the values of win of a = 0 and values of win of b = 3, so we can say that B will always win.
I have figure out my code with different test case but it is showing an error. so it will be great help if one will find defect in code.
# https://www.codechef.com/LRNDSA02/problems/PSHOT
def main():
# TODO: write code...
for T in range(int(input())):
N = 2*int(input()) - 1
S = input()
a,b = 0,0
for i in range(N+1):
if i%2 == 0 and S[i] == '1':
a+=1
if a <= (b + ((N-i)//2)+1) :
continue
else:
print(i)
break
else:
if S[i]=='1':
b+=1
if b <= (a + ((N-i)/2)+1) :
continue
else:
print(i)
break
if a == b:
print(N+1)
del S
if __name__ == '__main__':
main()
While submitting the code its giving wrong answer. Please anyone can tell me about missing test case since I have cross checked for lot of values.
def x(s):
a,b=0,0
for i in range(len(s)):
if(i%2==0):
if(s[i]=='1'):
a+=1
if(((((len(s)-1)-i)//2)+a) < b):
return i+1
if(i%2!=0):
if(s[i]=='1'):
b+=1
if(((((len(s)-1)-i)//2)+b) < a):
return i+1
return len(s)
for t in range(int(input())):
n=int(input())
s=input()
print(x(s))
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile!
Thanks!
Consider the test input:
1
3
111011
Thanks
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t–){
int n;
cin>>n;
string m;
cin>>m;
int rsA=n;
int rsB=n;
int s=0;
int diff=0;
for(int i=0;i<2*n;i+=2){
if((m[i]==‘1’&&m[i+1]==‘1’)||(m[i]==‘0’&&m[i+1]==‘0’)){
rsA–;
rsB–;
s+=2;
}
if(m[i]==‘1’&&m[i+1]==‘0’){
rsA–;
rsB–;
diff++;
s+=2;
}
if(m[i]==‘0’&&m[i+1]==‘1’){
rsA–;
rsB–;
diff–;
s+=2;
}
//cout<<rsA<<rsB<<s<<diff<<endl;
if(diff>0&&diff>rsB&&rsB!=0){cout<<s<<endl;break;}
else if(diff<0&&abs(diff)>rsA&&rsA!=0){cout<<s<<endl;break;}
else if((rsA==0&&rsB==0)&&diff!=0){
if(diff>0){cout<<s-diff<<endl;}
}
else if(diff==0&&(rsA==0&&rsB==0)){cout<<s<<endl;}
}
}
return 0;
}
pls help i have tried several test cases and they are working fine.
but its still showing wrong answer