#P8330. Maximizing Mode Frequency
Maximizing Mode Frequency
Maximizing Mode Frequency
Given a sequence \(a_1, a_2, \ldots, a_n\), you are allowed to perform exactly one operation: choose a contiguous interval \([l, r]\) with \(1 \le l \le r \le n\) and an integer \(k\) such that \(-10^9 \le k \le 10^9\), and add \(k\) to every element in that interval. In other words, for indices \(l \le i \le r\), the value becomes \(a_i+k\), while the others remain unchanged.
The goal is to make the sequence as "consistent" as possible. We define the mode as the number that appears most frequently in the final sequence. By carefully choosing a contiguous interval and an appropriate \(k\), you can convert a block (i.e. a contiguous segment) of identical numbers into another number, thereby increasing the frequency of that number. Note: In order for the operation on the chosen interval \([l,r]\) to set every element to a target number \(x\), the original numbers in that interval must be equal (say, all equal to \(v\)), and you must have \(x = v+k\) (with \(|x-v| \le 10^9\)).
Your task is to choose an optimal operation so that the mode frequency of the resulting sequence is maximized. Then output the maximum mode frequency as well as all possible values \(x\) (in increasing order) for which an optimal operation exists. In particular, if the sequence already has a number with high frequency, you may opt to perform an operation that does not change any numbers. Otherwise, you may choose an interval where all numbers are identical (but different from \(x\)) and convert them into \(x\) to add to the already existing occurrences of \(x\).
inputFormat
The first line contains an integer (n) ((1 \le n \le 10^5)). The second line contains (n) integers (a_1, a_2, \ldots, a_n) (each with absolute value at most (10^9)).
outputFormat
Output two lines:
- The first line is the maximum frequency of the mode after the operation.
- The second line contains the possible mode values that can be achieved in an optimal operation, separated by a single space, in increasing order.
sample
4
1 2 2 3
3
1 2 3
</p>