Group by ValueMedium

Group by Value

MediumPractice library · +20 XPhash mapgroupingarrays

Given an array of N integers, group the indices of the array by the value of the element at that index. For each distinct value present in the array, print the indices where it appears, in ascending order. The groups themselves should be printed in ascending order of the values they represent.

Input

The first line contains an integer N. The second line contains N space-separated integers, the elements of the array.

Output

For each distinct value in the input array, print a line. The line should start with the value, followed by a colon, a space, and then the indices (0-based) where this value appears, separated by spaces. The lines should be ordered by the distinct values in ascending order.

Constraints

1 <= N <= 200000. The values in the array are integers between -10^9 and 10^9.

Time limit 2 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
5
1 2 1 3 2
Output
1: 0 2
2: 1 4
3: 3

Value 1 appears at indices 0 and 2. Value 2 appears at indices 1 and 4. Value 3 appears at index 3. Groups are printed in ascending order of values: 1, 2, 3.

Example 2

Input
7
10 20 10 10 20 30 10
Output
10: 0 2 3 6
20: 1 4
30: 5

Value 10 at indices 0, 2, 3, 6. Value 20 at indices 1, 4. Value 30 at index 5. Groups printed for 10, 20, 30.

Hints