INTEST : Wrong Answer

#include
using namespace std;
int main(){
long long int n,k,ctr=0,*arr;
scanf("%I64d %I64d",&n,&k);
arr = new long long int[n];

for(long long int i=0;i<n;i++){
   scanf("%I64d",&arr[i]);
}

for(long long int j=0;j<n;j++){
   if(arr[j]%k==0){
      ++ctr; 
    }
}
 
printf("%I64d",ctr);
return 0;
} 

I can’t seem to figure out why this code is giving me a wrong answer on CodeChef ?

Two wrong things at the same time!

1.) scanf("%d%d", &n, &k); There should be two “%d”, u have to take values of both n and k, and ryt now you are taking only n.

2.) for(int i=0; i<=n :- Let me tell you, starting from i=0 to i=n, there are n+1 numbers, however you have to take only n numbers, So, “i<n” only.

So you are getting wrong answer because, k is taking value from the first element of the “so-typed-array” by you. Make the necessary changes.

2 Likes

Thanks ! I made the necessary corrections, but it still gives me a “Wrong Answer” on CodeChef. I also ran this code on Orwell Dev-C++ 5.7.1 - and it seems to be running fine on the test cases I tried it out on. Any suggestions ?

Woah! Hey i took ur code and made a small change. Now i am printing the elements as soon as they are being stored. U see this.

#include
using namespace std;
int main(){
long long int n,k,ctr=0,*arr;
scanf("%I64d %I64d",&n,&k);
arr = new long long int[n];

for(long long int i=0;i<n;i++){
  scanf("%I64d",&arr[i]);
  printf("%I64d\n",arr[i]);
}
 
for(long long int j=0;j<n;j++){
   if(arr[j]%k==0){
     ++ctr;
   }
}
 
printf("%I64d",ctr);
delete[] arr;
return 0;
}  

Just for testing.

1 Like

Now what happened is:- U chck it urself. Type

7 3
1
51
966369
7
9
999996
11

which is the sample case given in problem and check the result! :smiley: u will be shocked. Now let me tell you the reason. The problem lies with “%I64d”. Chnage it to “%lld” which is the standard specifier for long long integers. Ur output will be fine then.

1 Like

That works ! Thanks a lot ! By the way, Can you explain why %lld works, but not %I64d ?