Practice
Class Survey Vote Tally
2026/Feb/Mar·Variant 2·Q10·[15 marks]
HARDArrays
Run a class vote. Options are stored in Options[] and each voter's choice (the option number) in Votes[].
- Read the number of options (re-read until it is between 2 and 4) and the number of voters (re-read until it is between 2 and 6).
- Read each option name.
- For every voter, output the numbered list of options, then read a choice from 1 to the option count (re-read until valid).
- Count the votes for each option. Output the most popular option and its vote count, then the least popular option and its count. If there is a tie, keep the earliest option.
Input: Option count, voter count, option names, then each voter's choice. Output: The option list once per voter, then the most/least summary.
Example:
Input: 3
3
Apple
Banana
Cherry
1
1
2
Output: 1. Apple
2. Banana
3. Cherry
1. Apple
2. Banana
3. Cherry
1. Apple
2. Banana
3. Cherry
Most popular: Apple 2
Least popular: Cherry 0
Sample Test Cases
Test 1: Three options, Apple wins
Inputs: 3, 3, Apple, Banana, Cherry, 1, 1, 2
Expected: 1. Apple
2. Banana
3. Cherry
1. Apple
2. Banana
3. Cherry
1. Apple
2. Banana
3. Cherry
Most popular: Apple 2
Least popular: Cherry 0
Test 2: Invalid counts rejected, both votes for Blue
Inputs: 1, 2, 0, 2, Red, Blue, 2, 2
Expected: 1. Red
2. Blue
1. Red
2. Blue
Most popular: Blue 2
Least popular: Red 0