blog.itcode.devblog.itcode.dev

[Programmers / JAVA] Level 1 Mock Exam (42840)

Suja is short for someone who has given up on math. The Suja trio plan to guess every math answer on a mock exam. They mark their answers from question 1 to the last question as follows. Suja 1's pattern: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ... Suja 2's pattern: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ... Suja 3's pattern: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ... Given an array answers containing the correct answers from question 1 to the last question in order, write a solution function that returns, as an array, who got the most questions right.

[Programmers / JAVA] Level 1 Mock Exam (42840)

Suja is short for someone who has given up on math. The Suja trio plan to guess every math answer on a mock exam. They mark their answers from question 1 to the last question as follows. Suja 1's pattern: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ... Suja 2's pattern: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ... Suja 3's pattern: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ... Given an array answers containing the correct answers from question 1 to the last question in order, write a solution function that returns, as an array, who got the most questions right.
RWB0104
@RWBwritten at 2021-12-14 13:02:01
Programmers

시리즈 모아보기

Programmers

12 / 78
RankLanguage Used
Level 1

🖼️ JAVA

🔗 Mock Exam

Suja is short for someone who has given up on math. The Suja trio plan to guess every math answer on a mock exam. They mark their answers from question 1 to the last question as follows.

Suja 1's pattern: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...

Suja 2's pattern: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...

Suja 3's pattern: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

Given an array answers containing the correct answers from question 1 to the last question in order, write a solution function that returns, as an array, who got the most questions right.

  • The exam consists of at most 10,000 questions.
  • The correct answer for each question is one of 1, 2, 3, 4, 5.
  • If there are multiple people with the highest score, return the values sorted in ascending order.
answersreturn
{ 1, 2, 3, 4, 5 }{ 1 }
{ 1, 3, 2, 4, 2 }{ 1, 2, 3 }

Input/Output Example #1

  • Suja 1 got every question right.
  • Suja 2 got every question wrong.
  • Suja 3 got every question wrong.

So the person who got the most questions right is Suja 1.

Input/Output Example #2

  • Everyone got 2 questions right.
  • Suja 1's pattern - [ 1, 2, 3, 4, 5 ]
  • Suja 2's pattern - [ 2, 1, 2, 3, 2, 4, 2, 5 ]
  • Suja 3's pattern - [ 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 ]

Each Suja has a different pattern and a different pattern length, so we can't approach this carelessly. Since the length of each pattern differs, an index-based approach isn't suitable. It's more effective to circulate the pattern like a conveyor belt and compare the number at the front each time.

To do this, we need logic to circulate each Suja's pattern. [ 1, 2, 3, 4, 5 ] -> [ 2, 3, 4, 5, 1 ] This is very similar to the characteristics of a queue among data structures, so I plan to make active use of queues for this problem.

There are queues ONE, TWO, THREE defining each pattern, and they're initialized as follows.

JAVA

private void initQueue()
{
	ONE.clear();
	TWO.clear();
	THREE.clear();
	
	ONE.add(1);
	ONE.add(2);
	ONE.add(3);
	ONE.add(4);
	ONE.add(5);
	
	TWO.add(2);
	TWO.add(1);
	TWO.add(2);
	TWO.add(3);
	TWO.add(2);
	TWO.add(4);
	TWO.add(2);
	TWO.add(5);
	
	THREE.add(3);
	THREE.add(3);
	THREE.add(1);
	THREE.add(1);
	THREE.add(2);
	THREE.add(2);
	THREE.add(4);
	THREE.add(4);
	THREE.add(5);
	THREE.add(5);
}

Add the data to the queues in pattern order. The poll() method can be used to pull out the item at the front. With poll(), the item is removed at the same time it's retrieved.

After comparing the answer, put the used item back into the queue. This structure allows continuous circulation for each Suja regardless of the length of the exam.

JAVA

for (int item : answers)
{
	int one = Objects.requireNonNull(ONE.poll());
	int two = Objects.requireNonNull(TWO.poll());
	int three = Objects.requireNonNull(THREE.poll());
	
	counts[0] += item == one ? 1 : 0;
	counts[1] += item == two ? 1 : 0;
	counts[2] += item == three ? 1 : 0;
	
	ONE.add(one);
	TWO.add(two);
	THREE.add(three);
}

The answer comparison is as shown above. Objects.requireNonNull is there because poll() might potentially trigger a NullPointerException, and this measure removes the associated warning.

We pull the pattern value from each Suja's queue, compare it with the correct answer for the question, and if it matches, count it in the counts array.

You can see that the used pattern is put back into the queue via the add method.


Once all the scores are computed, find the highest score and return the Sujas who achieved that highest score.

Finding the maximum value in counts looks like this:

JAVA

int max = Arrays.stream(counts).max().getAsInt();

Then iterate over everyone who received the score equal to max and represent them as an array.

JAVA

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Queue;

/**
 * Mock Exam class
 *
 * @author RWB
 * @since 2021.12.10 Fri 21:43:26
 */
class Solution
{
	private static final Queue<Integer> ONE = new LinkedList<>();
	private static final Queue<Integer> TWO = new LinkedList<>();
	private static final Queue<Integer> THREE = new LinkedList<>();
	
	/**
	 * Method that returns the answer
	 *
	 * @param answers: [int[]] top scorers
	 *
	 * @return [int[]] the answer
	 */
	public int[] solution(int[] answers)
	{
		initQueue();
		
		int[] counts = { 0, 0, 0 };
		
		for (int item : answers)
		{
			int one = Objects.requireNonNull(ONE.poll());
			int two = Objects.requireNonNull(TWO.poll());
			int three = Objects.requireNonNull(THREE.poll());
			
			counts[0] += item == one ? 1 : 0;
			counts[1] += item == two ? 1 : 0;
			counts[2] += item == three ? 1 : 0;
			
			ONE.add(one);
			TWO.add(two);
			THREE.add(three);
		}
		
		int max = Arrays.stream(counts).max().getAsInt();
		
		ArrayList<Integer> list = new ArrayList<>();
		
		for (int i = 0; i < counts.length; i++)
		{
			// If this achieved the highest score
			if (counts[i] == max)
			{
				list.add(i + 1);
			}
		}
		
		return list.stream().mapToInt(Integer::intValue).toArray();
	}
	
	/**
	 * Queue initialization method
	 */
	private void initQueue()
	{
		ONE.clear();
		TWO.clear();
		THREE.clear();
		
		ONE.add(1);
		ONE.add(2);
		ONE.add(3);
		ONE.add(4);
		ONE.add(5);
		
		TWO.add(2);
		TWO.add(1);
		TWO.add(2);
		TWO.add(3);
		TWO.add(2);
		TWO.add(4);
		TWO.add(2);
		TWO.add(5);
		
		THREE.add(3);
		THREE.add(3);
		THREE.add(1);
		THREE.add(1);
		THREE.add(2);
		THREE.add(2);
		THREE.add(4);
		THREE.add(4);
		THREE.add(5);
		THREE.add(5);
	}
}
# Programmers# Algorithm# JAVA# Level 1
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08