Skip to main content
Back to problems
Codeforces
Easy
Arrays
Greedy
Sorting
Team Olympiad

Form teams of three students so that each team contains one member from each skill type 1, 2, and 3.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.

Teams

gfg
Problem Statement

Problem

You are given nn students. Each student has a skill type: $1, \2, or \3$.

Your task is to form as many teams as possible such that each team contains exactly three students and the three students on a team have different skill types — one student of type $1, one of type \2, and one of type \3$.

After choosing the teams, output the number of teams and the indices of the students in each team.

Goal

Maximize the number of valid teams.

Notes

  • Each student can belong to at most one team.
  • Any valid assignment with the maximum number of teams is accepted.

Input Format

  • The first line contains an integer nn.
  • The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n, where each ai{1,2,3}a_i \in \{1,2,3\}.

Output Format

  • On the first line, output the maximum number of teams.
  • Then output that many lines, each containing three integers: the indices of students in one team.
  • The order of teams and the order of indices inside a team do not matter, as long as every team is valid.

Constraints

  • 1n1001 \le n \le 100
  • ai{1,2,3}a_i \in \{1,2,3\}
Examples
Sample cases returned by the problem API.

Example 1

Input

7
1 3 1 3 2 1 2

Output

2
3 5 1
6 7 2

Explanation

There are three students of type 1, two of type 2, and two of type 3, so at most 2 teams can be formed. One valid choice is (3,5,1) and (6,7,2), where each team has one student of each type.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.