#K74707. Longest Contiguous Subarray with Uniform Frequencies

    ID: 34257 Type: Default 1000ms 256MiB

Longest Contiguous Subarray with Uniform Frequencies

Longest Contiguous Subarray with Uniform Frequencies

You are given an array of integers. Your task is to determine the length of the longest contiguous subarray where every distinct element appears the same number of times, and this common frequency must be greater than 1. In other words, in a valid subarray each element should appear more than once and with exactly equal counts. If no such subarray exists, the answer is 0.

For example, consider the array [1, 2, 2, 3, 3]. The subarray [2, 2, 3, 3] is valid because both 2 and 3 appear exactly 2 times, so the answer is 4. Conversely, in [1, 2, 3, 4], although all elements appear with frequency 1, the frequency is not greater than 1, hence the valid subarray length is 0.

inputFormat

The first line contains an integer T representing the number of test cases. For each test case, the first line contains an integer n denoting the number of elements in the array. The next line contains n space-separated integers representing the array.

outputFormat

For each test case, output a single integer on a new line: the maximum length of a contiguous subarray satisfying the condition. If no valid subarray exists, output 0.## sample

1
5
1 2 2 3 3
4