My issue
My code
#include <stdio.h>
int main(void) {
// your code goes here
int t,n,d[n],s=0;
scanf("%d",&t);
for(int i=0;i<t;i++){
scanf("%d",&n);
scanf("%d",&d[n]);
if(d[i]>=1000)
s=s+1;
}printf("%d",s);
return 0;
}
Problem Link: TODOLIST Problem - CodeChef
@shriyanshg9
In your code, there are a few problems i can see from a glance.
-
Array initialization: The way you have initialized the array is not correct. You should have taken the value of n (size of array) first before initializing the array. Otherwise a dynamically allocated array would have been fine too.
-
Taking value input in array: To take n inputs in the array, we would have needed a for loop like used in the code below. What you did was just take the first input value and initialized it as index n of the array.
-
Counter variable placement : Your counter variable s is initialized outside the test case loop and therefore will not reset to zero for each test case, rather it will keep increasing till whole program has been executed and thus provide a wrong result.
-
Printing outside test case loop: You have used printf outside the test case loop so, it will only print a final value of counter variable only once after all test cases have been executed. You should have used print function inside the test case loop to print result for each test case individuallly.
The following code worked for me and might help you too.
#include <stdio.h>
int main(void)
{
int t,n,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int a[n],c=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>=1000) c++;
}
printf("%d\n",c);
}
return 0;
}
1 Like
Try now? Changed condition from;
if(a[i]>999)
to
if(a[i]>=1000)
Submitted the code and it shows AC in both cases tho.