STUDVOTE - Editorial

PROBLEM LINK:

Practice
Contest

Author: Praveen Dhindhwa
Tester: Pushkar Mishra
Editorialist: Pushkar Mishra

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

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.

COMPLEXITY:

\mathcal{O}(N) per test case.

SAMPLE SOLUTIONS:

Author
Tester
Editorialist

1 Like

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++;

    }
    printf("%d\n",ctr);
}

}

Why am I getting an NZEC error.

Here’s my solution in JAVA: [https://www.codechef.com/viewsolution/10978093][1]

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!

https://www.codechef.com/viewsolution/11043846

https://www.codechef.com/viewsolution/11035046 please help. Not able to figure out whats wrong.

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! */

import java.util.;
import java.lang.
;
import java.io.*;

/* 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

Can someone find the problem in the following code?

import java.util.Scanner; class test2 { public static void main(String args[]) { Scanner s=new Scanner(System.in); int T=s.nextInt(); int temp,c; for(int a=1;a<=T;a++) { int N=s.nextInt(); int K=s.nextInt(); int A[]=new int[N]; c=0; for(int i=0;i=K) c++; } System.out.println©; } } }

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.

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
int hs[110];
for(int i=0;i<=109;i++)
hs[i]=0;
int a[n+5];
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]!=i && hs[a[i]]!=-1)
hs[a[i]]++;
else if(a[i]==i)
hs[a[i]]=-1;
}
int c=0;
for(int i=1;i<109;i++)
if(hs[i]>=k)
{
c++;
//cout<<“I=”<<i<<“hs[i]=”<<hs[i]<<endl;
}
cout<<c<<endl;
}
return 0;
}