You are not logged in. Please login at www.codechef.com to post your questions!

×

# STUDVOTE - Editorial

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

Cakewalk

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:

This question is marked "community wiki".

asked 30 Jul '16, 21:28 1.3k156581
accept rate: 4% 19.8k350498541

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=-1;c=-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--; } } }

(13 Aug '16, 12:01)

 0 Why am I getting an NZEC error. Here's my solution in JAVA: https://www.codechef.com/viewsolution/10978093 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? answered 31 Jul '16, 13:24 35●5 accept rate: 0%
 0 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 answered 06 Aug '16, 17:32 0★b02b01 1 accept rate: 0%
 0 https://www.codechef.com/viewsolution/11035046 please help. Not able to figure out whats wrong. answered 07 Aug '16, 08:23 1 accept rate: 0%
 0 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 answered 08 Oct '16, 20:19 0★zemu 1 accept rate: 0%
 0 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(c); } } } 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. answered 12 Nov '16, 16:29 1 accept rate: 0%

# include<bits stdc++.h="">

using namespace std; int main() { int t; cin>>t; while(t--) { int n,k; cin>>n>>k; int hs; 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; }

answered 25 Dec '16, 19:46 5★salman_1
1
accept rate: 0%

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&lt;n;j++){" scanf("%d="" ",&a[j]);="" a[j]="a[j]-1;" count[j]="0;" }="" for(j="0;j&lt;n;j++){" count[a[j]]++;="" }="" for(j="0;j&lt;n;j++){" if(count[j]="">=k && a[j]!=j) ctr++;

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


}

answered 31 Jul '16, 10:19 -1
accept rate: 0%

 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,852
×1,688
×968
×46
×3

question asked: 30 Jul '16, 21:28

question was seen: 3,816 times

last updated: 25 Dec '16, 19:46