Skip to main content
Back to problems
Codeforces
Medium
Arrays
Hash Maps
Jeff and Periods

Find all values that appear in the array as a run of equal numbers whose total length matches the value itself.

Acceptance 0%
Problem Statement

Problem

You are given an array of integers. A value is considered interesting if it appears in the array in one or more contiguous blocks, and the total number of times that value appears in the whole array is exactly equal to the value itself.

Your task is to output all interesting values in increasing order.

Clarification

If a number appears multiple times in different consecutive blocks, count all occurrences together. Only the final total frequency matters.

Goal

Identify every value xx such that the array contains exactly xx occurrences of xx.

Input Format

Input

  • The first line contains an integer nn — the size of the array.
  • The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n.

Output Format

Output

  • Print the number of interesting values.
  • Then print all interesting values in increasing order.

Constraints

Constraints

  • 1n1051 \le n \le 10^5
  • 1ai1091 \le a_i \le 10^9
Examples
Sample cases returned by the problem API.

Example 1

Input

7
4 5 4 5 4 5 5

Output

2
4 5

Explanation

The value 4 appears exactly 4 times, and the value 5 appears exactly 5 times. Both are interesting.

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.