PROBLEM LINK:
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.