I am solving Problem 'Red Light Green Light' with code 'DOLL' on Code chef.
Code Chef: Red Light Green Light
In this Problem, I need to take N inputs in one line. I tried using scanf() but to no avail, It didn't worked. It was talking N inputs in N line which isn't what I want. I tried using fgets and strtok. But There was some error coming which I am not understanding. So How can I do it?
void main(){
int T,N,K,sum_N=0;
int test_arr[N];
char name;
char *token;
scanf("%d",&T);
if(T<0||T>100000) printf("Wrong Input Cases"); // Condition Checking for T
if(sum_N>500000) printf("Exceeded Sum"); //Condition Checking for sum of N
while(T--){
scanf("%d %d",&N,&K);
if((N||K)<0||(N||K)>100000) printf("Wrong Values"); //Conditon Checking for N and K
// Here N Input in one Line are taken
fgets(name,sizeof(name),stdin);
token = strtok(name," ");
for(int i=0;i<N;i++){
test_arr[i] = *(token+i);
}
int ctr=0;
for(int i=0;i<N;i++){
if(test_arr[i]>K) ctr++;
}
printf("%d",ctr);
}
}

if((N||K)<0||(N||K)>100000)is not the way to test constraints.N||Kis always either0or1. in any case, they don't give you out of range values - they tell you what to expect.scanfwithfgets.fgetstakes a buffer of chars, not a single char. I think in this case, it is easier to just scan numbers withscanf("%d, ...)", otherwise you will have to make the line big enough to accomodate 10,000 ints.if(sum_N>500000) printf("Exceeded Sum");butsum_Nis always 0. Alsoint test_arr[N];is undefined behaviour becauseNhas an indeterminate value. Please don't try these problems as a learning tool - they are amusements for practised programmers. If you do, read the problem statement very carefully. Nowhere does it ask for the outputs you have given. And when you do output the answer to each test case there is no newline, unlike the example shown.