Given is an array A, A[i] tells us which person from 1 to N did the person i vote for to be good. We need to count the number of good people depending on these votes. A person is good if he got at least K votes AND didn’t vote for himself/herself.
EXPLANATION:
The problem is pretty straightforward for the given constraints. Since N \leq 100, we can keep an array Count to count the votes that each person got. After that, we just need to iterate over the array and check who all got at least K votes. If someone has got K votes or more, we check whether he/she voted for himself/herself or not. If not, then we can increase the count of good persons. Below is a pseudocode of the same:
countGoodPeople(A[], N):
for (i = 1 to N) {
Count[A[i]] = Count[A[i]] + 1; // increase the vote for the person who i voted for.
}
CountGood = 0;
for (i = 1 to N) {
if(Count[i] >= K and A[i] != i) {
CountGood = CountGood + 1;
}
}
return CountGood;
Please see tester’s/setter’s program for implementation details.
i still can’t understand what’s wrong in my solution. pls help.
#include<stdio.h>
main(){
int t,i,j;
for(i=0;i<t;i++){
int n,k;
scanf("%d %d",&n,&k);
//int z= n+1;
int a[n];
int count[n];
int ctr=0;
for(j=0;j<n;j++){
scanf("%d ",&a[j]);
a[j]=a[j]-1;
count[j]=0;
}
for(j=0;j<n;j++){
count[a[j]]++;
}
for(j=0;j<n;j++){
if(count[j]>=k && a[j]!=j)
ctr++;
I tried my code in BlueJ IDE with the sample cases given in the question and it worked perfectly.
Can someone explain me why it is happening or point out a mistake in my code?
[1]: https://www.codechef.com/viewsolution/10978093
Hi, I have compared my solution with others using C and still can’t find what’s wrong with it. I have tested my program on my Mac’s terminal and it produces the correct results.
So far, all my codes ends up with wrong answers or runtime errors on code chef. I have read the FAQ on that already though.
Hope someone can help me understand what’s wrong with my code. Thanks a lot!
Guys whats the problem with my code. I am getting Wrong Answer(WA) with subtask #1 being not implemented and subtask #2 being implemented but scored 0.
This is my first experience with codechef so please help me.
This is my program code:
/* package codechef; // don’t place package name! */
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int T=0,N=0,K=0,count=0;
T=sc.nextInt();
while(T<=100 && T>=1)
{
N=sc.nextInt();
K=sc.nextInt();
if(K>=N && (N<1 || N>100))
{
//do nothing.
}
else
{
int A[]=new int[N+1];
int c[]=new int[N+1];
A[0]=-1;c[0]=-1;
for(int i=1;i<=N;i++)
{
A[i]=sc.nextInt();
c[i]=0;
}
//input taken now running the processing of the program.
for(int i=1;i<=N;i++)
{ if(i==A[i])
{
c[i]=-1;
}
else
{
c[A[i]]++;
}
}
for(int i=1;i<=N;i++)
{
if(c[i]>=K)
{
count++;
}
}
}
System.out.println(count);
count=0;
T–;
}
}
}
i am getting the result correct when i execute the cod eon my pc
but on submission its result shows “Wrong Answer”
i tried the given test case example and even gave sample inputs i made up
the program worked as expected on my pc
please help
I have tested on ideone .com and bluej. It is working fine and giving the required output for the given input. but it is giving “wrong answer on codechef” and zero points.