What's wrong in the code?

Code-:CodeChef: Practical coding for everyone
Question-:JOHNY Problem - CodeChef

1
10
1 2 3 4 5 6 7 8 9 10
3

Edit: - he solved it.

2 Likes

Yes Sir but by help.
But I wanted to know the problem in the code typed by me :smile:

The problem with your code is here : (I have changed your logic)
for(int i=0;i<n;i++)
{
if(a[i]==c){
cout<<i+1<<endl;
break;
}
}
See, the problem with your logic was, even after finding the required index, its continuing in the loop and incrementing the count.
Rather I have used break to stop the further execution.
You can also do this :
for(int i=0;i<n;i++)
{
if(a[i]==c){
count++;
break;
}
else if(a[i]!=c){
count++;
}
}
Hope you got it :slightly_smiling_face:

2 Likes