int main(void) {
// your code goes here
int T;
scanf(“%d”, &T);
while (T–){
int N = 0;
scanf(“%d”, &N);
int size_S = N - 1;
int S[size_S];
for (int i = 0; i < size_S; i++){
scanf(“%d”, &S[i]);
}
int max = 1;
for (int i = 0; i < size_S; i++){
if (S[i] == 1 && S[i+1] ==0){
max ++;
}
}
printf(“%d\n”, max);
}
}
Why is it giving output as
1
1
1
for the input
3
2
0
7
000111
6
11100
whereas it is giving the correct output in other compilers. I’m unable to understand
Yes, I got it, thanks a lot. Now, I’m taking the input as a string and the code works perfectly: include <stdio.h>
int main(void) {
// your code goes here
int T;
scanf(“%d”, &T);
while (T–){
int N = 0;
scanf(“%d”, &N);
int size_S = N - 1;
char S[size_S];
scanf(“%s”, &S);
int max = 1;
for (int i = 0; i < size_S; i++){
if (S[i] == ‘1’ && S[i+1] == ‘0’){
max ++;
}
}
printf(“%d\n”, max);
}
}