Getting WA

Not able to figure what’s wrong…it’s cakewalk:(
https://www.codechef.com/problems/STUDVOTE

#include "bits/stdc++.h"
using namespace std;
int c[101];
int main() {
	int t;
	cin>>t;
	while(t--)
	{ 
	    memset(c,0,sizeof(c));
	    int n,k,ct=0;
	    
	    cin>>n>>k;
	    int a[n];
	    for(int i=0;i<n;i++)
	    {
	        cin>>a[i];
	        c[a[i]]++;
	        
	    }
	    for(int i=0;i<n;i++)
	    {
	        if(c[i]>=k && a[i]!=i+1)
	        ct++;
	    }
	    cout<<ct<<endl;
	}
}

Corrected the formatting. [The include statement was off]

Here is the bug-

Compilation Successful
    Input (stdin)
    1
    3 3
    1 1 1
    Your Output
    1

The problem states that since 1 voted for himself, he cannot be part of student council and hence the size should be 0. Implement this and check if it helps! :slight_smile:

Also, be careful that the number of votes should be ATLEAST K as in >=K.

EDIT- My submission with explanations here

1 Like

You have to correct the cases when person votes for himself.you can correct this by keeping track of people who voted for themselves and making count of those equal to zero and then checking that >=k condition this will get you an AC. :slight_smile:

1 Like

codechef temporarily out of service…will try

Refresh and try, its working fine for me :confused:

hey it happened to me just now…when I submitted code it showed me compilation error without any reason,and then after sometime it showed me WA on submitting same solution…

also on clicking “click here to view compilation error” it showed “try after sometime”.

That’s kind of…strange. Its still showing WA after fixing the bug?

c is a hash function you just need to check for 1 t0 100 in 2nd loop.and count all the students who have at least k votes. i want to put simple example like this take
1
5 3
100 100 100 90 90. this will give ans 1 but your program will give ans 0. so you have to make 2nd loop from 1 to 100. definitely it will help for you.

I just tried by putting an extra condition…but it didn’t work.
but thanks I have seen solution of another user…everything same except 1-based array indexing,where I used 0-based.

Yes, I was about to point it out. The question has used 1 based indexing and you have used 0 based indexing. Your code fails for this case-

Compilation Successful

Input (stdin)

1

3 1

1 1 2

Your Output

0

Expected Output

1

yes,I also used 1…101 in second loop,but ans was not correct at the compiler itself.

I Got AC. I just added 2 characters.

You are increasing votes for one index in advance. In line no. 17 you are incrementing c[a[i]]
but you should increment c[a[i]-1].

Here’s the AC solution link https://www.codechef.com/viewsolution/13199911

#include "bits/stdc++.h"
using namespace std;
int c[101];
int main() {
int t;
cin>>t;
while(t--)
{ 
    memset(c,0,sizeof(c));
    int n,k,ct=0;

    cin>>n>>k;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        c[a[i]-1]++;

    }
    for(int i=0;i<n;i++)
    {
        if(c[i]>=k && a[i]!=i+1)
        ct++;
    }
    cout<<ct<<endl;
   }
 }

EDIT:

In you code you are incrementing

c[a[i]]

but at the end you are comparing

if(c[i]>=k && a[i]!=i+1)

The input data is 1 index based and you are incrementing one-based index but comparing that with zero based cause your loop is

for(i=0;i<n;i++)

You can also get AC if you run your last loop from i=1;i<=n;i++ and compare a[i] with i

for(i=1;i<=n;i++)
{
      if(c[i]>=k && a[i]!=i)
      {
         //code goes here
       }
}

I hope this resolved your problem. Feel free to ask.

2 Likes

Oh…but why -1?

The -1 accounts for difference in indexing pattern. Our array does 0 based indexing while Q gives in 1 based.

@adhish_kapoor See the Edited answer.

Thanks@vijju123 @only4

2 Likes

You are using wrong index, comparing zero-based with one-based that’s it.