Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling.
Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the patty-cakes but to try not to eat the patty-cakes with the same filling way too often. To achieve this she wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them.
Pinkie Pie can eat the patty-cakes in any order. She is impatient about eating all the patty-cakes up so she asks you to help her to count the greatest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating!
Pinkie Pie is going to buy more bags of patty-cakes so she asks you to solve this problem for several bags!
Input
The first line contains a single integer T (1 ≤ T ≤ 100): the number of bags for which you need to solve the problem.
The first line of each bag description contains a single integer n (2 ≤ n ≤ 10^5): the number of patty-cakes in it. The second line of the bag description contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n): the information of patty-cakes' fillings: same fillings are defined as same integers, different fillings are defined as different integers. It is guaranteed that each bag contains at least two patty-cakes with the same filling.
It is guaranteed that the sum of n over all bags does not exceed 10^5.
Output
For each bag print in separate line one single integer: the largest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating for that bag.
For the first bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): 1, 6, 4, 7, 1, 6, 4 (in this way, the minimum distance is equal to 3).
For the second bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): 1, 4, 6, 7, 4, 1, 6, 4 (in this way, the minimum distance is equal to 2).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given T test cases. For each test case, we have n and an array a_i of length n. The values represent fillings. There is at least two patty-cakes with the same filling. We need to determine, over all possible orders of eating the cakes (i.e., permutations of the array), the maximum possible value of the minimum distance between two occurrences of the same filling. Distance between two eaten cakes with same filling is defined as the number of eaten cakes strictly between them.
We need to compute the largest achievable minimum distance.
This problem is reminiscent of scheduling items of various types (colors) such that the same type is spaced as far apart as possible. Typically, the problem of maximizing the minimum distance (or minimizing the maximum frequency) often involves the most frequent element. But here we have the ability to order arbitrarily. The objective is to maximize the minimum gap between consecutive occurrences of the same filling? Actually careful: The definition: "the minimum distance between the eaten with the same filling". That is, for each filling, consider all pairs of patty-cakes with that filling that are eaten consecutively? Or all pairs? The statement: "She wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them."
Interpretation: For a given order (a permutation), for each filling type (say value x), there are multiple occurrences. Consider all distances (number of other eaten cakes) between consecutive occurrences of that filling in the eating order? Or the minimal distance among all pairs of same-filling cakes? Usually, such problems refer to the minimal distance between any two same-filling cakes in the sequence (i.e., the smallest gap between two positions where the same filling appears). But the phrase "minimum distance between the eaten with the same filling" could be ambiguous. Look at the example.
Example 1: n=7, values: 1,7,1,6,4,4,6. Output: 3. They give order: 1,6,4,7,1,6,4. Let's compute distances between same fillings:
Filling 1: positions 1 and 5 (1-indexed). Between them: positions 2,3,4 => 3 cakes => distance 3.
Filling 6: positions 2 and 6: between positions 3,4,5 => 3 cakes => distance 3.
Filling 4: positions 3 and 7: between positions 4,5,6 => 3 cakes => distance 3.
Filling 7 appears only once; we ignore? But problem says each bag contains at least two cakes with the same filling, but there may be other fillings that appear only once? The input guarantee says "It is guaranteed that each bag contains at least two patty-cakes with the same filling." That doesn't forbid some fillings appearing once. For these, there is no distance. The minimum distance among all same-filling pairs would consider only fillings that appear at least twice. In the order given, all fillings with duplicates have distance 3, so minimum is 3. So they achieved minimum distance 3. Could we achieve 4? Possibly not. So output 3.
Filling 1: positions 1 and 6 => between positions 2-5 => count = 4? Actually between 1 and 6: positions 2,3,4,5 => 4 cakes => distance 4. Wait that would be 4, but min distance likely lower due to others.
Filling 4: positions 2,5,8? Actually order: 1 (pos1), 4 (pos2), 6 (pos3), 7 (pos4), 4 (pos5), 1 (pos6), 6 (pos7), 4 (pos8). So filling 4 at positions 2,5,8. Distances: between 2 and 5 => between 3,4 => 2 cakes => distance 2. Between 5 and 8 => between 6,7 => 2 cakes => distance 2. So min distance among these is 2.
Filling 6: positions 3 and 7 => between 4,5,6? Actually between 3 and 7: positions 4,5,6 => 3 cakes => distance 3.
Filling 7 only once.
So overall minimum distance is 2 (from 4's). So output 2.
Example 3: 3,3,3 output 0. With three identical cakes, any order will have two adjacent identical, distance 0? Order: 3,3,3: distances between first and second: 0 cakes between => distance 0. So output 0.
Example 4: 2 5 2 3 1 4 output 4. Let's examine: values: 2,5,2,3,1,4. Only duplicate is 2 (appears twice). Others are unique. So we need to order to maximize the distance between the two 2's. Since we can place them as far apart as possible: we have 6 cakes; if we put the two 2's at the ends, distance = number of cakes strictly between them = 4 (positions 2,3,4,5). So output 4 matches.
Thus interpretation: For each test case, we consider an ordering (permutation) of all cakes. For each filling value that appears at least twice, we consider the distances between consecutive occurrences? Or the minimal distance among any pair? In example 2, for filling 4, there are three occurrences, we computed distances between consecutive ones, and the min was 2. But could there be a non-consecutive pair that yields smaller distance? The distance between first and third (positions 2 and 8) would be 5 cakes between (positions 3-7) => 5, which is larger. So min is from consecutive. Typically, the minimal distance for a given value in a sequence is the minimal gap between any two occurrences; that is equivalent to the minimal distance between consecutive occurrences because the minimal over all pairs will be achieved by some consecutive pair (since if you have positions i < j < k, the distance j-i <= k-i, and similarly distances might be smaller between non-consecutive? Actually if you have positions sorted, the minimal distance between any two positions is the minimal difference-1. That is indeed the min over consecutive differences. So it's enough to consider consecutive occurrences.
Thus the problem: Given multiset of items with colors (fillings), we can arrange them in any order. Let the minimum over colors (that appear at least twice) of the minimal gap (number of items strictly between consecutive same-colored items) be as large as possible. Equivalent to maximize the smallest such gap.
We need to compute the optimal value.
Observation: This is akin to "round-robin" or "scheduling with spacing". Typically, to maximize the minimum gap, we want to spread out each color as evenly as possible. But we have multiple colors, and we need to arrange them interleaved. This looks like a problem of arranging items to avoid close repeats. Typically, the optimal arrangement is to sort by frequency and then place the most frequent items with as large gaps as possible, and then fill the rest. The metric we care about is the minimal distance between any two same-colored items. That is essentially the same as the "minimum distance between any two same symbols" in a string after rearrangement. There is known problem: "Rearrange array such that same elements are at least k distance apart" where we want to achieve distance at least k. Here we want to find maximum k such that arrangement exists.
Thus we can reframe: Given counts of each color, what is the largest d such that we can arrange the items so that any two items of same color are at least d+1 apart? Wait careful: If we require that the distance (number of items strictly between) is at least d, then the gap between positions is at least d+1 in terms of indices? Actually distance defined as number between. If two items are adjacent, distance = 0. If they have exactly one item between, distance = 1. So if we require minimum distance >= d, then for any same color, the number of items between any two occurrences must be at least d. That means in the sequence, between two same-colored items, there must be at least d other items. Equivalent to saying that same-colored items are at least d+1 apart in terms of positions.
So we want to find maximum d such that arrangement is possible with spacing constraint: same-colored items must be at least d+1 apart.
But the objective is to maximize the minimal distance over all pairs. So we want the largest d such that arrangement exists with all same-color distances >= d. That is exactly d_max.
So we can binary search d, and check feasibility.
Constraints: sum n ≤ 100,000, T up to 100, so binary search per test case O(n log n) maybe fine.
Feasibility check: Given counts c_i (for each distinct value). Want to arrange n items such that any two same items have at least d items between them (i.e., gap >= d+1). Equivalent to: in the sequence, if we place items one by one, we need to ensure that after placing an item of type i, we cannot place another item of type i until we have placed at least d other items. This is reminiscent of scheduling with cooldown. Classic problem: "Rearrange string k distance apart" (LeetCode 358, 621). For a single cooldown d (distance between same tasks must be at least d), the condition for existence is that the most frequent task count must satisfy: (max_count - 1) * (d+1) + (number of tasks with max_count) <= n. Actually for the problem of scheduling tasks with cooldown k (minimum interval between same tasks = k+1?), the classic formula: If the most frequent task occurs f times, then you need at least (f-1)(k+1) + 1 slots for them if you only have that task? Actually the problem: Given tasks A, B, etc., and a cooldown period of at least k (meaning between two same tasks there must be at least k other tasks), what is the minimum total time? Or to determine if arrangement is possible with given n? Typically, with idle slots allowed? But here we must place all tasks exactly, no idle slots. The condition for existence of a sequence without idle slots (i.e., we must fill all n positions with tasks) such that same tasks are at least d apart (i.e., distance between same tasks >= d) is something like: For each task with count c, the first occurrence can be at any position, then subsequent occurrences need to be at positions at least d+1 apart. This is like: we need to schedule tasks. The necessary and sufficient condition is: For all tasks, (c-1)(d+1) + 1 <= n? Actually if a task appears c times, you need at least (c-1)(d+1) + 1 positions to accommodate it, because you need d other items between each consecutive pair, so from the first to the last, the minimum total span is (c-1)(d+1) + 1. But that's a lower bound for the length of the sequence that contains that task c times with spacing d. However, if there are multiple tasks, the bound is more complex because the gaps can be filled by other tasks. The classic necessary condition for the "rearrange string k distance apart" problem: The maximum frequency f_max must satisfy: (f_max - 1) * (k+1) + count_of_max <= n, where count_of_max is the number of tasks having frequency f_max. This condition is known to be sufficient as well when we can reorder arbitrarily (no other constraints). Actually in LeetCode 358 "Rearrange String k Distance Apart", the solution uses a priority queue to place most frequent tasks, but the condition is: if (max_count - 1) * (k+1) + num_max <= n then possible. Let me recall: For problem "Given a string s and an integer k, rearrange the string so that the same characters are at least distance k from each other." The feasibility condition is: The most frequent character count must satisfy (max_count - 1) * k + max_count <= n? Wait there is confusion with definition of distance. In that problem, distance k means that there should be at least k characters between the same characters. So if distance k required, then gap (positions between) is at least k. So spacing between same characters is at least k+1 in terms of index difference. The typical formula for feasibility is: (max_count - 1) * (k+1) + count_max <= n? Let's derive:
Suppose we have a character with frequency f. To place them, you can think of putting f instances into slots. The minimal length required is: you need at least f-1 gaps of size k (i.e., at least k other characters between them). So the minimum total positions covered = (f-1)(k+1) + 1. Actually if you have f items and require at least k items between consecutive same, then the first one uses 1 slot, each next one requires k slots in between plus itself: So the total span from first to last = (f-1)(k+1) + 1. That's the minimal length of a contiguous segment that contains these f items with spacing constraint, but they don't have to be contiguous; they could be placed with larger gaps, but that only increases total needed length, so the minimal needed total number of positions in the whole sequence is at least that, because the span from the first occurrence to the last occurrence is at least that, and the total sequence length n must be at least that. However, this bound doesn't consider that other characters can occupy the gaps. Actually, if we place the first occurrence at some position, then the next must be at position >= first + k+1, and so on. So the last occurrence will be at position >= first + (f-1)(k+1). So the total length n must be at least (f-1)(k+1) + 1 (if we start at position 1). That's necessary for each f. But it's not sufficient for multiple high-frequency characters. The more precise condition: For the most frequent character(s), you need to intersperse them with others. Consider the classic problem: schedule tasks with cooldown k. The minimal total time (with idle allowed) is (f_max-1)(k+1) + n_max, where n_max is number of tasks with frequency f_max. But if n > that, you can fill the extra tasks in the idle slots or extend the end. But for arrangement without idle (i.e., we must fill exactly n slots), the condition is that n >= (f_max-1)(k+1) + n_max. This is known. Let's verify with examples.
Example: tasks A:3, B:3, n=6, k=1 (distance at least 1). Can we arrange? We need at least 1 between same. Feasible: A B A B? That's 4? Actually we have 6 tasks: A A A B B B. Need arrangement s.t. same letters at least one apart. Possible: A B A B A B -> works. According to condition: f_max=3, n_max=2 (since A and B both have 3). Compute (3-1)(1+1)+2 = 22+2=6. n=6, condition holds. So feasible.
Another: A:4, B:2, n=6, k=1. f_max=4, n_max=1 => (3)*(2)+1=7 >6 -> not feasible? Let's test: 4 As and 2 Bs, distance at least 1. Can we arrange? Attempt: A B A B A A? Last two As adjacent -> fails. A B A A B A? As at positions 1,3,4,6: distance between 3 and 4 is 0 -> fails. A A B A B A? adjacent As at 1&2 fail. It seems impossible. So condition holds.
What about distance definition? Our d corresponds to number of items strictly between; the required minimal distance is at least d. So that is exactly the same as k = d. So condition for feasibility of spacing at least d: (max_count - 1) * (d+1) + count_max <= n.
Check example 1: counts: 1:2, 7:1, 6:2, 4:2. max_count=2, count_max=3 (since 1,6,4 each have 2). n=7. Check for d=3: (2-1)(3+1)+3 = 14+3=7 <=7 -> feasible, so d=3 works. For d=4: (1)*5+3=8 >7 -> not feasible. So maximum d=3 matches.
Example 2: counts: 1:2, 4:3, 6:2, 7:1. max_count=3, count_max=1 (only 4 has 3). n=8. d=2: (3-1)(2+1)+1 = 23+1=7 <=8 -> feasible. d=3: (2)(4)+1=8+1? Wait (3-1)(3+1)+1 = 2*4+1=9 >8 -> not feasible. So max d=2 matches output.
Example 3: counts: 3:3. max_count=3, count_max=1, n=3. d=0: (3-1)(0+1)+1 = 21+1=3 <=3 feasible. d=1: (2)(2)+1=4+1? Actually 22+1=5 >3 not feasible. So max d=0 matches.
Example 4: counts: 2:2, others singletons. max_count=2, count_max=1 (only 2 appears twice). n=6. d=4: (2-1)(4+1)+1 = 15+1=6 <=6 feasible. d=5: 1*6+1=7 >6 not feasible. So max d=4 matches.
Thus the problem reduces to: Given frequencies of each filling, compute max d such that (max_freq - 1)*(d+1) + num_max_freq <= n. But careful: Is this condition both necessary and sufficient? Known from scheduling tasks with cooldown, it's sufficient. But we need to be sure for all cases. Let's test some edge cases.
Case: frequencies: A:2, B:2, C:2, D:1, n=7. max_freq=2, count_max=3. Condition for d: (1)(d+1)+3 <=7 => d+1+3 <=7 => d+4 <=7 => d <=3. So max d=3. Can we achieve distance 3? For 7 items with 3 pairs and one singleton, we need to place them such that each pair's two occurrences are at least 3 apart. Let's try: positions: we need to assign 2 of each of three types and one extra. A possible arrangement: A B C D A B C? Check distances: A at pos1 and pos5 => between 2,3,4 => 3; B at pos2 and pos6 => between 3,4,5 => 3; C at pos3 and pos7 => between 4,5,6 => 3; works. So d=3 works. d=4 would require (1)(5)+3=8>7 impossible.
Case: frequencies: A:5, B:2, n=7, distance d? max_freq=5, count_max=1. Condition: (5-1)(d+1)+1 <=7 => 4(d+1)+1 <=7 => 4d+4+1 <=7 => 4d+5 <=7 => 4d <=2 => d <=0.5 => d max =0? So only d=0 feasible. Indeed with 5 As and 2 Bs, you can't avoid adjacent As. So d=0.
Case: A:4, B:4, n=8. max_freq=4, count_max=2. Condition: (4-1)*(d+1)+2 <=8 => 3(d+1)+2 <=8 => 3d+3+2<=8 => 3d+5<=8 => 3d<=3 => d<=1. So max d=1. Can we arrange distance at least 1? We need at least 1 between same. Example: A B A B A B A B works, distances for A: positions 1,3,5,7: gaps of 1 between consecutive => distance =1? Actually between pos1 and pos3, there is one cake (pos2) so distance=1. So min distance =1. So d=1 works. Could we get d=2? Check: need gaps of at least 2 between same. For 4 As and 4 Bs total 8, if As need gaps of at least 2, the first A at position1, then next A at earliest position4, then at earliest 7, then at earliest 10 (exceeds length). So not possible. So condition correct.
Thus formula seems to work.
But we must consider that there may be fillings with counts less than max_freq. They don't affect the condition beyond the count_max? Actually the formula only uses max_freq and number of elements having that max_freq. This is typical for the problem "rearrange string so that same characters are at least k apart". It is known that a rearrangement exists iff (max_count - 1) * k + max_count <= n, but careful: In some formulations, k is the minimum distance (in terms of positions difference) required between identical characters. If k is the number of positions between, then the formula often is (max_count - 1) * (k+1) + count_of_max <= n. That's what we have.
But is there any scenario where condition holds but arrangement fails because of lower frequency items? I think the condition is sufficient. Because we can construct arrangement by placing the most frequent items first, with the required gaps, and then fill the remaining slots with other items. This is akin to the greedy algorithm for task scheduling with cooldown.
Proof sketch: Consider the sequence of positions. Place the max_freq items: they need to occupy positions such that they are at least d+1 apart. The minimal arrangement is to put them at positions 1, 1+(d+1), 1+2(d+1), ... up to count_max? Actually we have count_max items each with frequency max_freq. For each of those types, we need to schedule max_freq occurrences. The typical construction uses a priority queue, always placing the most frequent remaining tasks, ensuring cooldown. The condition ensures that idle slots (if any) can be filled.
But for a binary search answer, we can just compute d using the condition directly: d_max = floor((n - count_max) / (max_freq - 1)) - 1? Let's solve inequality:
(max_freq - 1)*(d+1) + count_max <= n
=> (max_freq - 1)*(d+1) <= n - count_max
=> d+1 <= (n - count_max) / (max_freq - 1)
=> d <= (n - count_max) / (max_freq - 1) - 1
Since d must be integer, d_max = floor( (n - count_max) / (max_freq - 1) ) - 1.
But we need to ensure d_max >= 0? Could be negative? If max_freq=1? But problem says there is at least one filling with at least two, so max_freq >=2. So denominator >=1. If result negative, then d_max might be -1? But d is non-negative integer distance (can be 0). For cases where formula yields negative, we should output 0? Let's check: If max_freq >=2 and count_max might be large. Example: n=2, both same? max_freq=2, count_max=1 => (2-1)(d+1)+1 <=2 => 1(d+1)+1 <=2 => d+1+1<=2 => d<=0 => d_max=0. So formula: (n - count_max)/(max_freq-1) -1 = (2-1)/1 -1 = 1-1=0. OK.
If max_freq=2, count_max=3, n=3? But that can't happen because if count_max=3, each appears twice? Actually count_max is number of distinct values that have frequency = max_freq. If max_freq=2, and count_max=3, then total items from these max_freq types is 23=6, but n=3? impossible. So n is at least max_freq * count_max? Actually n >= max_freq * count_max because those items exist. So n - count_max is at least (max_freq * count_max - count_max) = count_max(max_freq-1). So (n - count_max)/(max_freq-1) >= count_max, thus d_max >= count_max -1? Wait compute: (n - count_max) / (max_freq-1) >= (count_max*(max_freq-1))/(max_freq-1) = count_max. So d_max >= count_max -1. So it's non-negative.
But careful: The inequality is derived from feasibility of d; we want maximum d such that condition holds. But is the condition exactly the necessary and sufficient? I recall there is also the case when max_freq is high relative to n; the formula yields correct bound. However, I should verify with more complex examples.
Consider frequencies: A:3, B:3, C:2, D:1, n=9. max_freq=3, count_max=2. Condition: (3-1)(d+1)+2 <=9 => 2(d+1)+2 <=9 => 2d+2+2 <=9 => 2d+4 <=9 => 2d <=5 => d <=2.5 => d_max=2. Is it possible to have min distance 2? Let's try constructing: need A and B each 3, with spacing at least 2 (i.e., at least 2 others between same). We have 9 items. Try to arrange: One method: place A and B in a pattern: A _ _ A _ _ A ... but we need fill gaps. Since there are two types of max freq, we can interleave: A B ? A B ? A B ? Something. Let's attempt: positions: 1:A, 2:B, 3:C, 4:A, 5:B, 6:D, 7:A, 8:B, 9:C? Check distances: A at 1,4,7 => between 1-4: positions 2,3 => 2, 4-7: positions5,6 => 2 => distance 2. B at 2,5,8: between 2-5:3,4 => 2, 5-8:6,7 =>2. C appears twice at 3 and 9: between positions4-8 => many => distance >2. So min=2 works. So d=2 feasible. Could d=3 be feasible? Condition: 2(3+1)+2=2*4+2=10>9, so no. So max d=2.
So condition seems correct.
But wait, there might be nuance: The distance is defined as number of eaten cakes strictly between them. In our schedule, we considered only consecutive same-color occurrences. But what about non-consecutive? The minimal distance might be smaller if we consider non-consecutive pairs? Actually as argued, the minimum distance among all pairs of same color equals the minimum distance between consecutive occurrences because if positions are sorted, the differences between non-consecutive are larger. So it's fine.
But the problem statement says: "the minimum distance between the eaten with the same filling". It might be interpreted as, for each filling, compute the distance between every pair of patty-cakes of that filling, then take the minimum among all those distances, and then take the minimum over fillings. That is exactly what we did.
Thus the answer is maximum d such that we can arrange items with spacing at least d.
Now, we could binary search, but we can compute directly via formula: d_max = (n - cnt_max) // (max_freq - 1) - 1? Actually from inequality: (max_freq - 1)*(d+1) + cnt_max <= n.
=> d+1 <= (n - cnt_max) / (max_freq - 1)
Since d is integer, we can take d = floor((n - cnt_max) / (max_freq - 1)) - 1.
But careful: If (n - cnt_max) is exactly divisible, then d+1 = that quotient exactly? Actually we need d such that the inequality holds. For d = floor((n - cnt_max) / (max_freq - 1)) - 1, let Q = floor((n - cnt_max) / (max_freq - 1)). Then d+1 = Q? Actually d = Q - 1, so d+1 = Q. But Q <= (n - cnt_max) / (max_freq - 1). So (max_freq-1)Q <= n - cnt_max, thus (max_freq-1)(d+1) + cnt_max <= n. So inequality holds. For d = Q (i.e., d+1 = Q+1) we would have (max_freq-1)*(Q+1) + cnt_max = (max_freq-1)*Q + (max_freq-1) + cnt_max >? Since Q = floor(...), we have (max_freq-1)*Q <= n - cnt_max, but adding (max_freq-1) may exceed. So d_max is indeed Q-1.
But wait, we must check: Could there be a scenario where the condition is not tight, i.e., even if condition holds, arrangement might be impossible due to other constraints? I'd like to verify thoroughly with more tricky cases.
There's known result: Given frequencies, the maximum minimum gap achievable is floor( (n - max_count) / (max_count - 1) )? Or something like that? Actually there is known problem "Maximum Minimum Distance" on Codeforces? I've seen this problem: "Pinkie Pie and Party" maybe? Or it's Codeforces Round #... But I recall a problem: "Patty Cakes" maybe? Actually I think this is Codeforces problem 1393C? Or similar? The problem is called "Pinkie Pie Eats Patty-cakes"? I recall a Codeforces problem: "Pinkie Pie Eats Patty-cakes" from Round #... The solution is to binary search or use formula. Let me search memory: There's Codeforces Round #1185 (Div. 3) problem C? Actually there is problem "Pinkie Pie Eats Patty-cakes" from Round #1462? Not sure. But I've seen solutions: The answer is (n - max_count * number_of_max?) Wait, I recall a solution: The answer is (n - cnt_max) // (max_freq - 1) - 1. Actually I think the known answer is (n - cnt_max) // (max_freq - 1). But let's check with examples: Example1: max_freq=2, cnt_max=3, n=7 => (7-3)//(2-1)=4//1=4, then maybe minus something? 4? But output is 3. So that would be 4-? Actually (n - cnt_max) // (max_freq - 1) = (7-3)//1 =4. Then if we say answer = that - 1 =3. That matches. Example2: (8-1)//(3-1)=7//2=3, minus 1 =2. Example3: (3-1)//(3-1)=2//2=1, minus1=0. Example4: (6-1)//(2-1)=5//1=5, minus1=4. So formula holds. So answer = (n - cnt_max) // (max_freq - 1) - 1.
But wait, is it always minus 1? Could it be that answer = (n - cnt_max) // (max_freq - 1) - 1? Let's derive properly.
We have inequality: (max_freq - 1)*(d+1) + cnt_max <= n.
=> d+1 <= (n - cnt_max)/(max_freq - 1)
Since d is integer, the maximum integer d such that d+1 <= floor? Actually we need d such that (max_freq - 1)*(d+1) + cnt_max <= n. Let d+1 = k, an integer >=1? d>=0 => k>=1. Then we need (max_freq - 1)*k + cnt_max <= n => k <= (n - cnt_max) / (max_freq - 1). The maximum integer k satisfying this is k_max = floor((n - cnt_max) / (max_freq - 1)). Then d_max = k_max - 1.
But we must ensure d_max is non-negative. In some cases, k_max could be 0? But k = d+1, so k >= 1 because d >=0. However, if floor(...) = 0, then k_max=0, but then d_max = -1, which would be invalid. But can that happen? Let's see: n - cnt_max is at least (max_freq * cnt_max - cnt_max) = cnt_max*(max_freq - 1). Since cnt_max >=1, (n - cnt_max) >= cnt_max*(max_freq - 1) => (n - cnt_max)/(max_freq - 1) >= cnt_max. Since cnt_max>=1, floor >=1. So k_max >=1. So d_max >=0. Good.
Thus answer is simply computed.
But caution: This formula assumes that the feasibility condition is exactly this inequality. I've seen some solutions to similar problems using binary search but the direct formula is used after counting max frequency and number of max.
However, I recall a Codeforces problem: "C. Pinkie Pie Eats Patty-cakes" (maybe from Round #1462 Div. 3?). The solution indeed is: Let maxf = maximum frequency, cnt = count of numbers with frequency = maxf. Then answer = (n - cnt) // (maxf - 1) - 1? Let's search memory: I think I've seen solution: answer = (n - cnt) / (maxf - 1) - 1. Wait, the formula in many editorials is: answer = (n - cnt) // (maxf - 1). But then for example 1: (7-3)//(2-1)=4, but answer is 3. So maybe they define distance differently? Let's check. Possibly they define distance as the number of cakes between including something? Or maybe they define minimum distance as the index difference minus one, which is what we use. If they output (n - cnt) // (maxf - 1), then example 1 would output 4, but actual output is 3. So maybe they meant something else.
Let me search memory: There is a problem "C. Pinkie Pie Eats Patty-cakes" from Codeforces Round #1462? Actually Round 1462 is "Divide and Summarize"? Not sure. I recall a problem "B. Pinkie Pie Eats Patty-cakes" but I'm not certain. I will think: In the notes, they gave order for first test: 1,6,4,7,1,6,4. The min distance is 3. If we computed (n - cnt) // (maxf - 1) = (7-3)//1=4. So that doesn't match.
Maybe the correct formula is (n - cnt) // (maxf - 1) - 1? That matches.
But let's test with other known examples: Suppose frequencies: A:4, B:2, C:2. n=8, maxf=4, cnt=1. (8-1)//(4-1)=7//3=2, minus1=1. Can we achieve min distance 1? We have 4 As and 2 Bs, 2 Cs. Can we arrange such that minimum distance at least 1? Probably yes: A B A C A B A C? That yields As at positions 1,3,5,7, distances 1. So min=1. Could we get distance 2? Condition: (4-1)(2+1)+1=33+1=10>8, so no. So answer 1 matches.
What about frequencies: A:3, B:3, C:3, n=9. maxf=3, cnt=3. (9-3)//(3-1)=6//2=3, minus1=2. Is min distance 2 possible? Let's try to arrange: We need 3 of each A,B,C with spacing at least 2. Try pattern: A B C A B C A B C. Check distances: A at 1,4,7 => between: positions 2,3 for 1-4 (2 items) -> distance 2; 4-7: positions5,6 -> distance 2. So min=2. Works. Could we get distance 3? Condition: (3-1)(3+1)+3=24+3=11>9, no. So answer 2.
Now consider frequencies: A:5, B:5, C:5, n=15. maxf=5, cnt=3. (15-3)//(5-1)=12//4=3, minus1=2. So answer 2. Check: can we achieve distance 2? That would require spacing at least 2 between same. With 5 each, total 15, maybe pattern: A B C A B C A B C A B C A B C? That uses 15 positions. Distances for A: positions 1,4,7,10,13 => differences: 3 => between 2 items => distance=2. So min=2. Could we get 3? Condition: (5-1)(3+1)+3=44+3=19>15, no. So correct.
Now consider frequencies: A:6, B:5, n=11. maxf=6, cnt=1. (11-1)//(6-1)=10//5=2, minus1=1. So answer 1. Check: Can we arrange 6 As and 5 Bs such that min distance between As at least 1? Possibly: A B A B A B A B A B A -> works, distances between As: positions 1,3,5,7,9,11: diff 2 => distance 1. So min=1. Could we get distance 2? Condition: (6-1)(2+1)+1=53+1=16>11, no.
Now consider frequencies: A:4, B:4, C:3, D:2, n=13. maxf=4, cnt=2. (13-2)//(4-1)=11//3=3, minus1=2. So answer 2. Check if possible? Should be.
Seems consistent.
But is there any scenario where the condition holds but arrangement fails due to lower frequencies causing conflicts? Let's think more deeply.
We need to arrange items of various frequencies such that each type i with frequency f_i has spacing >= d. This is equivalent to scheduling tasks with cooldown d. The classic necessary and sufficient condition for existence of a schedule with no idle slots (i.e., exactly n tasks) is: max frequency f_max satisfies (f_max - 1)(d+1) + count_max <= n. This is known from the "rearrange string k distance apart" problem where k = d. Actually, for the problem of rearranging string such that identical letters are at least distance k apart (with k being the minimum gap in terms of index difference? Let's confirm: In Leetcode 358, "Rearrange String k Distance Apart", it says: "Given a non-empty string s and an integer k, rearrange the string such that the same characters are at least distance k from each other." Typically distance means the absolute difference in indices; if two same characters must be at least k apart, then the number of characters between them is at least k-1. They ask for rearrangement if possible. The solution often uses priority queue and the condition is: (max_count - 1) * k + count_of_max <= n? Or (max_count - 1) * (k) + count_of_max? Let's check: If k=2, then they need to be at least 2 apart (positions difference >=2). That means at least 1 other character between them. So the gap (number between) is >=1. In our terminology, d = number between = k-1? Actually if positions differ by at least k, then number between = k-1. So if k=2, d=1. In our condition, we used (max_freq-1)(d+1) + count_max <= n. For d=1, that's (max_freq-1)2 + count_max <= n. In the LeetCode problem with k=2, the condition is (max_freq-1)2 + count_max <= n? Or maybe (max_freq-1)k + count_max <= n? Let's test: Suppose max_freq=3, count_max=1, n=5, k=2. (3-1)2+1=5 <=5, so should be feasible. Example: "aaabc" with k=2: can we rearrange to have same letters at least 2 apart? One possible: "abaca"? Actually "abaca" has a at positions 1,3,5 -> differences 2 and 2 -> distance (positions diff) =2 -> okay. So condition works. If they used (max_freq-1)k + count_max, for k=2 that's 22+1=5, same. For d=1, (max_freq-1)(d+1)+count_max = (max_freq-1)2+count_max, so matches (max_freq-1)k+count_max if we set k = d+1. So indeed the condition is same but with k = d+1. In LeetCode 358, the typical condition is (max_count - 1) * (k) + count_of_max <= n? I've seen both. Actually I recall: For the problem "Task Scheduler" (LeetCode 621) where you have cooldown n (minimum interval between two same tasks), the minimum time to schedule all tasks is max( (max_count - 1)(n+1) + count_max, total_tasks). So that formula gives the length when you include idle slots. For arranging without idle, we need total_tasks >= that bound? Actually if total_tasks is exactly that bound, then you can arrange with no idle slots? Wait, the formula for minimum time with idle slots is: (max_count - 1)(n+1) + count_max, where n is cooldown. That's the length of schedule including idle if necessary. If total tasks exceed that, you can fill extra tasks into idle slots, and the length becomes total_tasks. So the condition for a schedule with no idle (i.e., arrangement of tasks within total_tasks slots) is that total_tasks >= (max_count - 1)(cooldown+1) + count_max. Because if total_tasks is larger, you can fill the extra tasks in the idle slots, but the length becomes total_tasks, but you still might have idle? Actually if total_tasks > that bound, you can schedule all tasks without idle? Let's recall: In Task Scheduler, you can always schedule tasks without idle by placing extra tasks in the gaps, but the minimal length becomes total_tasks if total_tasks is larger than the bound. So the bound is a lower bound on length. So if total_tasks >= that bound, you can always schedule without idle, i.e., produce a sequence of length total_tasks that satisfies cooldown. In other words, it's possible if and only if total_tasks >= (max_count-1)(cooldown+1) + count_max. So this is exactly our condition with cooldown = d (since d is number of other tasks between, so cooldown = d?). Wait: In task scheduler, cooldown is the number of units you must wait before running same task again. If cooldown = k, then between two same tasks there must be at least k other tasks (or idle). So that matches our d exactly: d = cooldown. So the condition is total_tasks >= (max_count-1)(d+1) + count_max. So it's the same. And it's both necessary and sufficient.
Thus our derived formula is correct.
Hence the answer for each test case is:
Count frequencies of each value.
Find max_freq = maximum frequency.
Count how many values have frequency = max_freq, call it cnt_max.
Answer = (n - cnt_max) // (max_freq - 1) - 1.
But wait: What about the case where max_freq = 1? Then denominator zero. But guarantee says at least two same filling, so max_freq >=2. So safe.
Now, is there any nuance with multiple max_freq? Already accounted.
Thus we can compute O(n) per test case.
But let's double-check the derivation with a more complex scenario: Suppose we have frequencies: A:4, B:4, C:3, D:2, E:2, F:1, n=16? Actually sum = 4+4+3+2+2+1 = 16. max_freq=4, cnt_max=2 (A,B). Compute answer: (16-2)//(4-1)=14//3=4, minus1=3. So answer would be 3. Can we achieve min distance 3? Let's test via condition: (4-1)(3+1)+2 = 34+2=14 <=16, so feasible. Let's attempt construction: Need to schedule A and B each 4 times with spacing at least 3 between same. Also C 3, D 2, E 2, F 1.
We can use the typical method: Create a list of slots for the max_freq items: We need to place the 4 occurrences of A and 4 of B. With cooldown 3, the minimal schedule for just these would be: positions: we can think of "frames" of size d+1=4. Usually, for tasks with cooldown d, you can think of having (max_freq-1) blocks of size (d+1) plus one extra slot for each of the count_max items. Actually the bound (max_freq-1)(d+1)+cnt_max is the minimal length to schedule the max_freq tasks. In our case, that's 34+2=14. So within 14 slots, we can place all A and B satisfying spacing. Then we have 2 extra slots (since n=16) that can be filled with the other tasks anywhere, potentially breaking some gaps but not reducing the minimum distance below d? Actually if we insert extra tasks between same-colored tasks, the distance increases, so fine. If we insert them before or after, also fine. However, we must ensure that we don't violate spacing for the other tasks. The other tasks have lower frequencies, so they are easier to satisfy. And we can always insert them into gaps without causing conflict as long as we don't put two of same too close. But we have to schedule all tasks. The classic greedy algorithm: sort by frequency descending, then fill slots in cycles. It works if condition holds.
So I'm confident.
But before finalizing, let's test a boundary: Suppose n=2, both same: max_freq=2, cnt_max=1. Answer = (2-1)//(2-1) -1 = 1//1 -1 = 1-1=0. Correct.
Suppose n=4, with values: A:2, B:2. max_freq=2, cnt_max=2. Answer = (4-2)//(2-1)-1 = 2//1 -1 =2-1=1. Can we achieve min distance 1? Order: A B A B => distances: A at 1,3 => between 1 cake => distance 1. B at 2,4 => distance 1. So min=1. Could we get distance 2? That would require placing A and B with at least 2 others between same. With 4 items, impossible. So correct.
Suppose n=5, with frequencies: A:3, B:2. max_freq=3, cnt_max=1. Answer = (5-1)//(3-1)-1 = 4//2-1 =2-1=1. Check condition for d=1: (3-1)(1+1)+1=22+1=5 <=5 -> feasible. Can we arrange to have min distance 1? Example: A B A B A? Check A at positions 1,3,5: between 1-3: pos2 => distance 1; 3-5: pos4 => distance 1. So min=1. d=2 would require (3-1)(2+1)+1=23+1=7>5, not feasible. So correct.
Now, what about scenario where max_freq is very high relative to n? Example: n=100, max_freq=99, cnt_max=1 (one element appears 99 times, another appears once). Answer = (100-1)//(99-1)-1 = 99//98-1 = 1-1=0. So min distance 0. Indeed, with 99 of one and 1 other, you can't avoid adjacent same? Actually you could place the single other in between some, but you still have many same, they will be adjacent because there aren't enough others to separate them by at least 1. With 99 of A and 1 of B, to have distance at least 1 between As, you'd need at least 98 others between the 99 As (since between each consecutive A need at least 1 other). That would require at least 98 others. You have only 1 other, so impossible. So min distance 0.
Now, consider scenario where there are many max_freq items, but n is just enough. e.g., n=6, frequencies: A:3, B:3. max_freq=3, cnt_max=2. Answer = (6-2)//(3-1)-1 = 4//2-1 =2-1=1. That gives 1. But we earlier computed for 3 each, 6 total, answer should be 1? Actually for 3 each we computed earlier for n=6? Wait earlier we had example A:3, B:3, n=6. Let's compute min distance: With 3 each, we can arrange A B A B A B, distances for A: positions 1,3,5 => between 1-3: 1 cake => distance 1; 3-5: also 1. So min distance 1. Could we get distance 2? Condition: (3-1)(2+1)+2 = 23+2=8>6, so no. So answer 1. But our formula gave (6-2)//(2)-1 =4//2-1=2-1=1, correct.
Now for A:3, B:3, C:3, n=9, answer=2 as earlier.
Thus formula works for these.
But wait, is the answer always integer? Yes.
Now, what about scenario where (n - cnt_max) // (max_freq - 1) yields a value but then minus 1 gives something that might not be optimal? Let's test with random small cases by brute force to be sure. Since sum n is 100k, but we can test small n manually.
We should also verify the condition for cases with more than one max_freq and also lower frequencies that are numerous.
Consider n=10, frequencies: A:4, B:3, C:3. max_freq=4, cnt_max=1. Answer = (10-1)//(4-1)-1 = 9//3-1=3-1=2. So d=2. Condition check: (4-1)(2+1)+1=33+1=10 <=10 -> feasible. Can we arrange? Let's try to construct: Need 4 As with at least 2 others between each. That means gaps of at least 2. With 4 As, the minimal span is (4-1)3+1=10, exactly n. So they must occupy positions such that the gaps are exactly 2 between them. So the positions could be 1,4,7,10 (differences 3). Then we need to place B and C (3 each) in the gaps. The gaps are between A1-A2: positions 2,3; A2-A3: positions 5,6; A3-A4: positions 8,9. That's 6 slots, exactly we have 3 B and 3 C =6 items. So we can place them, but we must ensure B's and C's own spacing constraints: B appears 3 times, but we have at most one B per gap? Actually B's three occurrences could be placed in three different gaps, each gap only holds 2 slots, but B cannot be in adjacent if we have only one per gap? Since B's occurrences need to be at least d=2 apart. If we place B in positions 2,5,8, then distances: 2->5: gap of positions 3,4 => 2, okay. 5->8: positions 6,7 => 2. So fine. Similarly C in positions 3,6,9. So arrangement possible. So d=2 works. Could we get d=3? Condition: (4-1)(3+1)+1=3*4+1=13>10, not possible. So answer 2 correct.
Now consider n=10, frequencies: A:4, B:4, C:2. max_freq=4, cnt_max=2. Answer = (10-2)//(4-1)-1 = 8//3-1 = 2-1 =1. So d=1. Check condition: (4-1)(1+1)+2 = 32+2=8 <=10, feasible. d=2: (4-1)(2+1)+2=33+2=11>10, not feasible. So answer 1. Let's try to construct d=1: We need As and Bs each 4, with at least 1 other between same. Minimum span for just those: (4-1)2+2=32+2=8. So we can place them in 8 slots, leaving 2 slots for C's. One possible arrangement: A B A B A B A B ? ? . That's 8 positions filled with A B alternating. But we have 4 As and 4 Bs, so alternating gives A B A B A B A B. Check distances: A at 1,3,5,7: consecutive gaps: between 1-3: position2 -> distance 1; 3-5: position4 ->1; 5-7: position6 ->1. B similar. So min=1. Then we need to insert two C's. They must not violate spacing for themselves: C appears twice, need distance at least 1. We can insert C's in the two extra slots at the end (positions 9,10). But then distance between C's: only one C? Actually if we put C at 9 and 10? They would be adjacent (distance 0) which violates d=1? Since we require min distance >=1 for all colors. So we need to place the two C's such that there is at least 1 other between them. Since we only have two extra slots, we could insert them not both at the end but maybe break the pattern: For instance, arrange as A B A B A B A C B C? Let's try: We need total 10 positions. Let's design: Use the minimal schedule for A and B with cooldown 1: they occupy 8 slots in an interleaved way. To place two C's with spacing at least 1, we can insert them into the gaps such that they are separated by at least one other. Since we have many gaps, we can put one C between some A and B, and another C elsewhere, ensuring at least one between them. For example: A C B A B A B A B? That gives 4 As? Actually we must preserve counts. Let's systematically construct: We have 4 As, 4 Bs, 2 Cs. We need to produce sequence of length 10 with spacing >=1 for each. Try: A B A B A C B A B C? Check counts: A: positions 1,3,5,8 => that's 4 As. B: positions 2,4,7,9 => 4 Bs. C: positions 6,10 => distance between C's: positions 6 and 10 have positions 7,8,9 between => 3, so >=1. Check spacing for A: positions 1 and 3: gap 1 (position2) ok; 3 and 5: gap 1 (pos4) ok; 5 and 8: gap positions 6,7 => 2 >=1 ok. B: 2 and 4: gap1; 4 and 7: gap 5,6 =>2; 7 and 9: gap8 =>1. All good. So d=1 works. Could we achieve d=2? We already saw condition fails. So answer 1 is correct.
Now, what about scenario where there are many different values with max_freq, and the answer might be influenced by the total number of items? The formula seems solid.
But I recall a different solution: Some people might compute answer as: (n - maxf * cnt) // (maxf - 1) ??? Let's test: For example 1: maxf=2, cnt=3 => maxf*cnt=6, (7-6)//(2-1)=1//1=1, not 3. So no.
Maybe alternative: (n - cnt) // (maxf) ? Example 1: (7-3)//2=4//2=2, not 3. So no.
Thus our formula seems correct.
But wait, the problem statement says: "She wants the minimum distance between the eaten with the same filling to be the largest possible." Could "minimum distance" be interpreted as, for each filling, compute the minimum distance over all pairs of patty-cakes of that filling, then take the minimum over fillings. That is what we did. However, is there any nuance about "distance between two patty-cakes the number of eaten patty-cakes strictly between them"? That is exactly our d.
Now, is the answer always floor((n - cnt_max) / (max_freq - 1)) - 1? Let's confirm via derivation from known result: In the task scheduler problem, the minimal time needed is max( (max_freq-1)(cooldown+1) + cnt_max, n). If we have total tasks = n and cooldown = d, the condition for feasibility (i.e., can schedule within n time without idle?) is that (max_freq-1)(d+1) + cnt_max <= n. So the maximum d such that holds is floor((n - cnt_max) / (max_freq - 1)) - 1. Indeed.
But is the condition also sufficient when there are multiple tasks with frequencies less than max_freq? Yes, because we can always insert them into the schedule without violating constraints, as long as we maintain the ordering of the max_freq tasks. This is a known fact: The greedy algorithm works.
Thus we can just compute the answer directly.
Now, let's check the problem constraints: sum of n <= 1e5, T <= 100. So O(n) per test case is fine. We just need to compute frequencies.
Edge Cases:
All cakes are same: then max_freq = n, cnt_max = 1. Answer = (n-1)//(n-1)-1 = 1-1=0. That's correct (distance 0, as adjacent same).
Two types, each appear n/2 times (n even). Then max_freq = n/2, cnt_max = 2 if both equal? Actually if both appear exactly n/2, then max_freq = n/2, cnt_max = 2. Answer = (n - 2) // (n/2 - 1) - 1. For n=6, (6-2)//(3-1)-1 = 4//2-1=2-1=1. For n=8, (8-2)//(4-1)-1 = 6//3-1=2-1=1. For n=10, (10-2)//(5-1)-1 = 8//4-1=2-1=1. So answer 1 always? But can we achieve larger if n/2 is big? Let's test n=10, each 5. Condition for d=2: (5-1)(2+1)+2 = 43+2=14>10, not feasible. So yes, max d=1. So alternating pattern yields distance 1. Could we achieve distance 2? No, because need more separators.
If there is one type with high frequency and many other types: e.g., max_freq = 10, cnt_max=1, n=20. Answer = (20-1)//(10-1)-1 = 19//9-1 = 2-1=1. So d=1. Condition: for d=1: (10-1)2+1=92+1=19 <=20, feasible. d=2: (9)*3+1=27+1=28>20, not feasible. So min distance 1. With 10 A and 10 others, we can achieve distance 1 by interleaving A with others: A X A X ... eventually maybe A A at end? But we have exactly 10 others, so we can place A and other alternating, but since count equal, we can do A O A O ... A O? That would require 10 As and 10 others, perfect alternating. In that pattern, distance between As is 1 (one other). So yes.
If max_freq is just slightly above half, answer might be 0? Example: n=5, frequencies: A:3, B:2. Answer = (5-1)//(3-1)-1 = 4//2-1 =2-1=1, not 0. Actually 3 out of 5, we can still have min distance 1 as earlier. So only when max_freq > (n+1)/2? Let's check: Condition for d=0 always holds because (max_freq-1)(0+1)+cnt_max = max_freq -1 + cnt_max <= n? Since max_freq <= n, and cnt_max >=1, this may be >n? Actually for d=0, we require (max_freq-1)1 + cnt_max <= n => max_freq + cnt_max -1 <= n. Since cnt_max >=1, max_freq could be n and cnt_max=1, then n +1 -1 = n <= n, holds equality. So d=0 always feasible. So answer at least 0. For d=1, condition is (max_freq-1)2 + cnt_max <= n. Rearranged: 2max_freq -2 + cnt_max <= n => 2max_freq <= n + 2 - cnt_max. Since cnt_max >=1, 2max_freq <= n+1. So if 2max_freq > n+1, then d=1 not feasible, so answer 0. So answer can be 0 when max_freq > (n+1)/2. For instance, n=5, max_freq=3, cnt_max=1: 23=6 > 5+1=6? Actually 6 >6? Not strictly >; condition for d=1 requires 2*max_freq <= n+1? Let's compute: need (max_freq-1)2 + cnt_max <= n. For max_freq=3, cnt_max=1: (2)2+1=5 <=5, so d=1 feasible. So 2max_freq =6, n+1=6, so equality works. So d=1 works. For max_freq=3, cnt_max=2? That would be 22+2=6? Actually if max_freq=3, cnt_max=2 (two types each appear 3, total n at least 6). For n=6, condition d=1: (2)*2+2=6 <=6 works. So d=1 works. For d=2 would need (2)*3+2=8>6 fail. So answer 1.
When does answer become 0? When (max_freq-1)1 + cnt_max > n? But as argued, max_freq + cnt_max -1 > n. Since n >= max_freq * cnt_max? Actually total n >= max_freq * cnt_max because those items alone sum to that. So max_freq * cnt_max <= n. Is it possible that max_freq + cnt_max -1 > n? For cnt_max >=2, max_freq * cnt_max >= 2max_freq. But max_freq + cnt_max -1 could be less than that. Let's see if there exist parameters where d=0 condition fails? d=0 condition is (max_freq-1)1 + cnt_max <= n => max_freq + cnt_max -1 <= n. Since n >= max_freq * cnt_max? Not necessarily; total n includes also items with lower frequencies, so n is at least max_freq * cnt_max? Actually the total number of items is sum of all frequencies. The items with frequency max_freq account for max_freq * cnt_max. But there could be other items, so n >= max_freq * cnt_max. So max_freq + cnt_max -1 <= max_freq * cnt_max? Not always. For max_freq >=2, cnt_max >=1. Compare max_freq * cnt_max vs max_freq + cnt_max -1. For cnt_max=1, max_freq 1 = max_freq, and max_freq +1 -1 = max_freq, so equality holds, condition holds (since n >= max_freq). For cnt_max=2, max_freq2 vs max_freq+2-1 = max_freq+1. Since max_freq>=2, max_freq2 >= max_freq+1 (since 2max_freq - (max_freq+1) = max_freq-1 >=1). So n >= 2max_freq, which is >= max_freq+1, so condition holds. In general, max_freqcnt_max >= max_freq + cnt_max -1 for max_freq>=2 and cnt_max>=1? Let's check: inequality max_freqcnt_max >= max_freq + cnt_max -1 rearranged to max_freqcnt_max - max_freq - cnt_max +1 >=0 => (max_freq-1)(cnt_max-1) >=0. That's true. So indeed, (max_freq-1)(cnt_max-1) >= 0. So max_freqcnt_max >= max_freq + cnt_max -1. Since n >= max_freq*cnt_max, we have n >= max_freq + cnt_max -1. So d=0 condition always holds. So answer is always at least 0, which matches expectation: you can always arrange in some order (like group by color) yields min distance 0. So answer can't be negative. Good.
Thus the formula yields non-negative.
Now, before finalizing, let's double-check with more random small cases by writing a brute-force to verify. I'll mentally test some.
Case: n=5, values: [1,1,2,2,3]. Frequencies: 1:2, 2:2, 3:1. max_freq=2, cnt_max=2. Answer = (5-2)//(2-1)-1 = 3//1-1=3-1=2. So predicted answer 2. Can we achieve min distance 2? Let's try to find an order: Need to place the two 1s at least 2 apart, and two 2s at least 2 apart. With n=5, the maximum distance between two items is 3 (if placed at ends). But we need both pairs to have distance >=2. Let's attempt: 1 3 1 2 2? Check: 1s at pos1 and pos3: between pos2 => distance 1 (only one cake), fails. 1 2 3 1 2? 1s at pos1 and pos4: between pos2,3 => 2 cakes, distance 2; 2s at pos2 and pos5: between pos3,4 => 2 cakes, distance 2. So works! min distance =2. Could we get distance 3? For d=3, condition: (2-1)(3+1)+2 = 14+2=6 >5, not feasible. So answer 2 correct.
Case: n=7, values: 1,1,2,2,3,3,4. Frequencies: each of 1,2,3 appear twice, 4 once. max_freq=2, cnt_max=3. Answer = (7-3)//(2-1)-1 = 4//1-1=4-1=3. We already have example 1 with 3 pairs and one singleton, answer 3. Works.
Case: n=8, values: 1,1,1,2,2,2,3,3. Frequencies: 1:3, 2:3, 3:2. max_freq=3, cnt_max=2. Answer = (8-2)//(3-1)-1 = 6//2-1=3-1=2. So answer 2. Let's test via condition: d=2: (3-1)(2+1)+2 = 23+2=8 <=8, feasible. d=3: 2*4+2=10>8. So max d=2. Can we construct? Need to arrange three 1s and three 2s with spacing at least 2, plus two 3s. Minimal span for 1s and 2s: (3-1)3+2 = 23+2=8, exactly n. So the positions for 1s and 2s must be exactly spaced: suppose we set positions for 1s at 1,4,7 and 2s at 2,5,8? That would be 1 and 2 interleaved? But then 1s at 1,4,7 have gaps: between 1 and 4: positions 2,3 -> 2, ok; between 4 and 7: 5,6 ->2. 2s at 2,5,8: gaps: 2-5: 3,4 ->2; 5-8:6,7 ->2. So okay. But we also need to place two 3s. However we have no extra slots because total n=8 and we already placed 6 items? Wait we placed 1s and 2s: that's 6 items. But we used positions 1,2,4,5,7,8? Actually if we set 1s at 1,4,7 and 2s at 2,5,8, that's 6 positions (1,2,4,5,7,8). We need to place two 3s as well, but we have only positions 3 and 6 left. So that's perfect: 3 at 3 and 6. Check distances for 3s: between positions 3 and 6: positions 4,5 => 2, >=2. So all good. So d=2 works.
Thus formula passes.
Now, I'm comfortable.
But wait, there is another nuance: The distance is defined as the number of eaten cakes strictly between them. So if we have three occurrences of the same filling, the distance between the first and third is not considered for the minimum? Actually the minimum over all pairs. But as argued, the minimum will be between some consecutive occurrences. So it's fine.
Count frequencies using dictionary or array (since a_i ≤ n, but values up to n, but could be up to n. However sum n over test cases ≤ 100k, we can use dictionary for simplicity.
Determine max_freq = max(freq values).
Determine cnt_max = number of keys with freq == max_freq.
Test1: n=7, max_freq=2, cnt_max=3 => (7-3)//1 -1 =4-1=3. OK.
Test2: n=8, max_freq=3, cnt_max=1 => (8-1)//2 -1 =7//2=3 -1=2? Wait 7//2=3 (floor division), then -1=2. OK.
Test3: n=3, max_freq=3, cnt_max=1 => (3-1)//2 -1 =2//2=1 -1=0. OK.
Test4: n=6, max_freq=2, cnt_max=1 => (6-1)//1 -1 =5-1=4. OK.
Thus matches.
Double-check integer division: In Python, // is floor division, which works since all are positive.
Edge Cases: What if max_freq - 1 = 1 and (n - cnt_max) is something? Works.
Now, is there any case where answer could be larger than n? Obviously not, since max distance possible is n-2 (if only two of a kind and all others are different, you could place them at ends giving distance n-2). The formula yields at most (n - 1)//1 -1 = n-2? For max_freq=2, cnt_max=1, answer = (n-1)-1 = n-2, which matches maximum possible. Good.
Now, I'll think about possibility of off-by-one: For max_freq=2, cnt_max=1, we get ans = n-2. That matches our example 4 (n=6 => 4). Good.
For max_freq=2, cnt_max=2, answer = (n-2)//1 -1 = n-3. For n=4, answer 1 (as earlier). Could we achieve n-3? For n=5, with two pairs and one singleton? Example: n=5, two pairs (A,B) and one C. max_freq=2, cnt_max=2. Answer = (5-2)//1 -1 = 3-1=2. Is that achievable? Let's test: A, B appear twice each, C once. Need min distance >=2 for both A and B. Can we arrange? Try: A C A B B? That fails B's distance? B's at pos4,5: distance 0. Try A B C A B? A at 1,4: between 2,3 =>2; B at 2,5: between 3,4 =>2; C singleton. So min distance =2 works. Could we get distance 3? Condition: (2-1)*(3+1)+2=4+2=6>5, so no. So ans 2 correct. And n-3=2, matches.
For max_freq=2, cnt_max=3, answer = (n-3)//1 -1 = n-4. For n=7, answer 3 (7-4=3). Good.
For max_freq=3, cnt_max=1, answer = (n-1)//2 -1. For n=7, that's (6)//2 -1 =3-1=2? But let's test a scenario: n=7, frequencies: A:3, others: four singletons? Actually we need total n=7. So A appears 3 times, and we have 4 distinct other items each once. Then max_freq=3, cnt_max=1. Answer = (7-1)//2 -1 = 6//2-1=3-1=2. Can we achieve min distance 2? Need to place three A's with at least 2 others between each. The minimal span for A's would be (3-1)3+1 = 23+1=7. So they would need to occupy positions 1,4,7 if we use exactly 7 positions and gaps of 2. Then we have 4 singletons to fill positions 2,3,5,6. That's possible, and the singletons are all distinct so no conflict. So min distance =2. Could we get distance 3? Condition: (3-1)(3+1)+1 = 24+1=9 >7, no. So answer 2 correct.
Now, what about scenario where max_freq=3, cnt_max=2, n=8? Already computed answer = (8-2)//2 -1 =6//2-1=3-1=2. That matched earlier.
All good.
Now, final answer: For each test case, compute as above.
But I recall another approach: The answer is also equal to (n - maxf * cnt) // (maxf - 1)? Let's test: (n - maxfcnt)//(maxf-1) maybe? For test1: maxf=2, cnt=3 => 23=6, (7-6)//1=1, not 3. So no.
Thus the formula we have is correct.
Now, one more subtlety: The input guarantee says each bag contains at least two patty-cakes with the same filling. So there is at least one value with frequency >=2, so max_freq >=2. Good.
Now, we might need to consider that the answer is defined as "the largest minimum distance" and we computed d such that there exists arrangement with min distance >= d. But is it guaranteed that the maximum achievable minimum distance is exactly the maximum d such that arrangement exists? Yes, because if we can achieve d, we can certainly achieve any smaller d? Actually if we can achieve min distance >= d, then the actual min distance in that arrangement is at least d, but could be larger. So the maximum possible min distance is the largest d for which there exists an arrangement with min distance at least d. So binary search approach would find that maximum. And we derived that it's the largest integer satisfying (max_freq-1)*(d+1)+cnt_max <= n. So solving that inequality gives d_max.
Thus answer is as given.
Now, code: Since sum n <= 1e5, we can read all test cases, process each.
Implementation:
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
arr = [int(next(it)) for _ in range(n)]
from collections import Counter
freq = Counter(arr)
max_freq = max(freq.values())
cnt_max = sum(1 for v in freq.values() if v == max_freq)
ans = (n - cnt_max) // (max_freq - 1) - 1
out.append(str(ans))
sys.stdout.write("\n".join(out))
But we must be careful: (max_freq - 1) could be zero if max_freq=1, but that won't happen due to guarantee. Still, we can assert.
Now, I'll test with the sample.
One more thought: Is there any scenario where the answer computed by formula is not integer? No, we use integer division.
But wait, we derived d_max = floor((n - cnt_max) / (max_freq - 1)) - 1. However, is it possible that the maximum d is actually floor((n - cnt_max) / (max_freq - 1)) without minus 1? Let's test with a simple case: Suppose n=2, max_freq=2, cnt_max=1. Then (n - cnt_max)/(max_freq - 1) = (1)/(1) = 1. floor =1. Subtract 1 gives 0. That matches answer (0). If we didn't subtract 1, would be 1, which is too high because you can't have distance 1 with only 2 cakes (distance would be number of cakes strictly between them, which would be 0). So minus 1 is needed.
Now consider n=3, frequencies: two of one type and one other: max_freq=2, cnt_max=1. Then (3-1)//(2-1)=2//1=2, minus1=1. Is min distance 1 possible? With items A,A,B. To achieve min distance 1 between the two As, we need at least 1 cake between them. With B as the only other, we can arrange A B A, distance =1. So answer 1 correct. If we didn't subtract 1, would be 2, but distance 2 is impossible (would require placing As at ends with 2 between, but only one other cake). So minus 1 correct.
Now consider n=4, max_freq=2, cnt_max=2 (two pairs). (4-2)//1 =2, minus1=1. We saw earlier min distance 1 possible, but could we get distance 2? To get distance 2, need As with two between them: would need at least 3 other cakes between? Actually distance 2 means at least 2 cakes between. With n=4, the maximum possible distance between two items is 2 (if placed at positions 1 and 4, between 2,3 => distance 2). But we have two pairs; if we place A at 1 and 4, distance 2; B at 2 and 3, distance 0. So min distance 0. To have both pairs distance >=2, we would need to arrange A at 1,4 and B at 2,? Actually B needs two positions apart, but if A at 1,4, then B could be at 2 and ? The only remaining positions are 3? Actually if A at 1 and 4, used positions 1,4, remaining 2,3. B at 2 and 3 would be adjacent => distance 0. So impossible. Could we arrange A at 1,3 and B at 2,4? Then A distance 1, B distance 1. So min=1. So max=1. So formula correct.
Now, there is an alternative derivation: Let maxf = max frequency, k = number of colors with maxf. Then the answer = (n - k) // (maxf - 1) - 1. That's what we have.
Now, is there any scenario where the answer is not simply this due to the requirement that the minimum distance is taken over all fillings, but maybe fillings with lower frequency might have smaller distance than d even if the condition for maxf holds? Could that happen? Let's think: In the scheduling, we arrange to satisfy spacing for all tasks. The greedy algorithm ensures all tasks meet the cooldown. The condition ensures that the most constrained tasks (maxf) can be placed. The less frequent ones have smaller or equal frequency, so they are easier to schedule. However, is it possible that due to the arrangement, a lower frequency task ends up having distance smaller than d? For instance, consider d=2, and we have a task with frequency 2. We need to place them at least 2 apart. In our constructed schedule, we typically place maxf tasks first with gaps, then insert others. When we insert lower frequency tasks, we must also respect their cooldown. But since they have lower frequency, the required cooldown is the same d, but they have fewer occurrences, so they are easier to place. However, we must be careful not to place two of the same low-frequency task too close. But if we use a greedy that always picks the most frequent available tasks, it naturally respects cooldown. The known result is that if the condition holds, there exists a valid arrangement for all tasks. So no issue.
Thus answer is correct.
Now, let's ensure we handle large inputs. We'll use fast I/O.
Now, we should also consider if there is any alternative interpretation that could break our formula. Let's read the problem statement again carefully: "Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the patty-cakes but to try not to eat the patty-cakes with the same filling way too often. To achieve this she wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them."
The phrase "minimum distance between the eaten with the same filling" could be interpreted as: For each filling, consider the distances between every pair of cakes with that filling, take the minimum for that filling, and then take the minimum over all fillings. That's what we used.
But could it be interpreted as: For the entire eating order, consider all pairs of cakes that have the same filling, compute the distance for each pair, and then take the minimum among all those distances (i.e., the smallest gap between any two same-filling cakes, regardless of filling). That's the same as we did, because we take the minimum across all fillings anyway; it's equivalent.
Now, could it be interpreted as: She wants to maximize the minimal distance between consecutive occurrences of the same filling? That is also the same as the minimum gap between any two same-filling cakes, since the smallest gap will be between some consecutive occurrences. So all interpretations converge.
Thus solution is correct.
Now, we can write code.
But we must note: Input values a_i are up to n, but not necessarily consecutive. Use dictionary.
Also, note sum n <= 1e5, so O(n) per test case is fine.
One more thing: There is a known Codeforces problem: "C. Pinkie Pie Eats Patty-cakes" from Round #1462? Actually I recall a problem with same wording. The editorial indeed gives answer = (n - cnt) // (maxf - 1) - 1? Let's check memory: I think I've seen a solution where answer = (n - cnt) / (maxf - 1) - 1. Yes.
Alternatively, some solutions compute answer by binary search and check condition using the formula: (max_freq - 1) * mid + max_freq <= n? Actually they might have used mid as distance, and condition: (max_freq - 1) * (mid + 1) + count_max <= n. That's equivalent.
Now, we must be careful with integer division: (n - cnt_max) // (max_freq - 1) could be computed as integer division; then subtract 1.
But what if (n - cnt_max) is negative? Not possible because n >= cnt_max? Since cnt_max is number of colors with max frequency, each has at least one occurrence, so cnt_max <= n. Also max_freq >=2, so denominator positive. So fine.
Now, test with some random large numbers to ensure no overflow (Python fine).
Thus final solution.
However, I want to prove the formula more rigorously for completeness. But since we are just coding, it's okay.
One more edge: If max_freq = n (all same), then cnt_max = 1, answer = (n - 1) // (n - 1) - 1 = 1 - 1 = 0. Good.
If max_freq = n-1 (one appears n-1 times, one other appears once), cnt_max = 1, answer = (n - 1) // (n-2) - 1. For n>2, (n-1)//(n-2) = 1 (since n-1 < 2*(n-2) for n>=3? Actually for n=3, (2)//1=2? Wait n=3, max_freq=2 (since n-1=2), cnt_max=1, (3-1)//(2-1)=2//1=2, minus1=1. But earlier we said for n=3 with two As and one B, min distance can be 1? Actually order A B A gives distance 1. So answer 1. Works. For n=4, max_freq=3, cnt_max=1, (4-1)//(3-1)=3//2=1, minus1=0. So answer 0. With three As and one B, can we achieve min distance >=1? We need As spaced at least 1 apart. With three As and one B, can we arrange? Possibility: A B A A? That has adjacent As at end. A A B A? adjacent As at start. A B A A? same. A A B A? same. The only arrangements: A A B A (positions 1,2,4): As at 1 and 2 distance 0. A B A A (1,3,4): As at 3 and 4 distance 0. B A A A (2,3,4): adjacent As. So indeed min distance 0. So answer 0 correct.
Thus formula passes.
Now, what about scenario where max_freq = (n+1)//2 + something? Already covered.
Thus final answer.
Now, let's code.
But note: We need to compute cnt_max: number of distinct fillings that have frequency equal to max_freq. That includes possibly many. Since values up to n, we can just iterate over frequencies.
Implementation details:
Use collections.Counter to count frequencies.
max_freq = max(counter.values())
cnt_max = sum(1 for v in counter.values() if v == max_freq)
That's O(n).
Now, output.
Now, we must ensure we handle multiple test cases efficiently.
I'll produce final solution.
But I'll also consider if any alternative approach is needed for some corner case where max_freq - 1 = 0? Not possible.
Now, we can finalize.
Wait, I just realized: The input says "1 ≤ a_i ≤ n". So values are within 1..n. That means we could use an array of size n+1 for frequencies to be faster than dictionary. Since sum n ≤ 1e5, dictionary is fine. But using list might be slightly faster. We'll use list.
We can do:
freq = [0] * (n+1) # but careful: n differs per test case, we can allocate each test case.
But using Counter is simpler.
Given constraints, both okay.
Now, final answer.
But before concluding, I want to test a tricky case: Suppose n=10, frequencies: A:4, B:4, C:2. We computed answer = (10-2)//3 -1 =8//3=2 -1=1. So answer 1. But could we achieve min distance 2? Let's test manually: need As and Bs with spacing at least 2. Minimal span for As and Bs together? Let's compute condition: (max_freq-1)(d+1)+cnt_max = (4-1)(2+1)+2 = 3*3+2=11 >10, so no. So answer 1 seems correct. Construct for d=1: A B A B A B A B ? ? and then place C's, need to ensure C's spacing >=1. We have two C's, need at least one between them. With 8 slots used by A and B alternating, we have two extra slots. If we put both C's in the extra slots, they could be adjacent if we put them consecutively, violating d=1. But we can insert one C in between A and B somewhere, and the other at the end, ensuring at least one between them. For example: A B A B A B C A B C? Let's try to produce a valid sequence: We need 4 As, 4 Bs, 2 Cs. Let's attempt: A C B A B A B A B? That gives A count? Let's systematically: We'll use pattern with A and B interleaved but insert Cs into gaps. Since d=1, we can place two Cs separated by at least one. One possibility: A B A B A B A C B C. Count: A: positions 1,3,5,7 => 4; B: positions 2,4,6,9 => 4; C: positions 8,10 => distance between 8 and 10: position9 between => 1, ok. All distances: between As: 1-3: pos2 =>1; 3-5: pos4 =>1; 5-7: pos6 =>1. B: 2-4: pos3 =>1; 4-6: pos5 =>1; 6-9: positions 7,8 =>2 => >=1. So min=1. So d=1 works.
Thus formula holds.
Now, I'm satisfied.
Now, we should produce final answer.
But wait, there's one more nuance: The distance is defined as number of eaten cakes strictly between them. In our arrangement, we consider the entire sequence. For fillings that appear only once, there is no pair, so they don't affect the minimum (since there is no distance to consider). The minimum is only over fillings that appear at least twice. That is consistent with our condition: we only need to ensure spacing for those with frequency >=2. For singletons, they trivially satisfy any spacing.
In the condition, we only considered max_freq, but we need to consider all frequencies. However, lower frequencies might be easier. So condition is sufficient.
Now, confirm that there is no scenario where a lower frequency item could force a smaller minimum distance than d even if the condition for max_freq holds, and we might be able to arrange differently to avoid that, but the condition might be necessary but not sufficient? But as argued, it's both.
Alright.
Now, final code.
But hold on: The problem statement might be from Codeforces Round #1462 (Div. 2) problem C. I recall that problem exactly: "Pinkie Pie Eats Patty-cakes". I'll quickly search memory: I think the solution indeed is (n - maxCount * countMax?) No, I think I recall editorial: answer = (n - cnt) // (mx - 1) - 1. Yes, that seems familiar.
Thus answer.
Now, produce final solution.
One more thing: The input might have multiple test cases. We'll read everything.
Now, final Python code.
But we need to be careful about reading input: T up to 100, n up to 1e5, sum n <= 1e5, so reading all into list is fine.
Now, output.
I'll write:
import sys
from collections import Counter
def solve():
data = sys.stdin.buffer.read().split()
t = int(data[0])
idx = 1
out = []
for _ in range(t):
n = int(data[idx]); idx+=1
arr = list(map(int, data[idx:idx+n])); idx+=n
freq = Counter(arr)
max_freq = max(freq.values())
cnt_max = sum(1 for v in freq.values() if v == max_freq)
ans = (n - cnt_max) // (max_freq - 1) - 1
out.append(str(ans))
sys.stdout.write("\n".join(out))
if name == "main":
solve()
Edge Cases: Ensure integer division works correctly. For Python 3, // does floor division, but since numbers are positive, it's fine.
Now, test with sample.
We'll run mental: Already did.
Thus final answer.
But before finalizing, I'd like to double-check with a random brute force for small n to be absolutely sure. I'll try a few small n manually.
We can attempt to brute force all permutations for n up to maybe 7 and compute maximum min distance, then compare to formula.
Let's do mental test with n=6, values: [1,1,2,2,3,3] (three pairs). max_freq=2, cnt_max=3. formula: (6-3)//1 -1 =3-1=2. So answer 2. Can we achieve min distance 2? Let's try to arrange: Need each pair at least 2 apart. With 6 items, maximum distance between two items is 4 (if placed at ends). But we need three pairs each with distance >=2. Let's attempt: 1 2 3 1 2 3? Check: 1 at pos1,4: between 2,3 => 2, ok; 2 at pos2,5: between 3,4 => 2; 3 at pos3,6: between 4,5 => 2. So min=2 works. Could we get distance 3? Condition: (2-1)(3+1)+3 = 14+3=7 >6, so no. So answer 2 correct.
Now, n=6, values: [1,1,2,2,3,4] (two pairs, two singles). max_freq=2, cnt_max=2. formula: (6-2)//1 -1 =4-1=3. So answer 3. Can we achieve min distance 3? Need both 1s and 2s to have distance >=3. With 6 items, to have distance 3 between two items, they must be placed with exactly 3 between: positions 1 and 5 (gap 3), or 2 and 6 (gap 3). So we could place 1s at 1 and 5, 2s at 2 and 6? That would give 1s distance 3, 2s distance 3? Check 2s at 2 and 6: between positions 3,4,5 => 3, ok. Then we have 3 and 4 to place at positions 3 and 4. But then 3 and 4 are singletons, no problem. So arrangement: 1,2,3,4,1,2? Actually that's 1 at 1, 2 at 2, 3 at 3, 4 at 4, 1 at 5, 2 at 6. Check: 1s at 1,5 => between 2,3,4 => 3; 2s at 2,6 => between 3,4,5 => 3; min=3. So works. Could we get distance 4? Condition: (2-1)*(4+1)+2 = 5+2=7>6, so no. So answer 3 correct.
Now, n=5, values: [1,1,2,2,3] (two pairs, one singleton). formula: max_freq=2, cnt_max=2? Actually 1 and 2 each appear twice, so cnt_max=2. (5-2)//1 -1 =3-1=2. So answer 2. We earlier found arrangement 1 2 3 1 2 gives min distance 2. Could we get distance 3? Condition: (2-1)*(3+1)+2 = 4+2=6>5, no. So answer 2 correct.
Now, n=5, values: [1,1,1,2,3] (triple, and two singles). max_freq=3, cnt_max=1. formula: (5-1)//(3-1)-1 =4//2-1=2-1=1. So answer 1. Can we achieve min distance 1? We need three 1s with at least 1 between each. With 5 items, we can do 1 2 1 3 1: positions 1,3,5: gaps: between 1-3: pos2 =>1; 3-5: pos4 =>1. So min=1. Could we get distance 2? Condition: (3-1)(2+1)+1 =23+1=7>5, no. So answer 1.
Now, n=5, values: [1,1,1,2,2] (triple and pair). max_freq=3, cnt_max=1? Actually max_freq=3 (for 1), but 2 appears twice. So cnt_max=1 because only 1 has frequency 3. formula: (5-1)//(3-1)-1 =4//2-1=2-1=1. So answer 1. Can we achieve min distance 1? Need 1s spaced at least 1 apart, and 2s spaced at least 1 apart. Try: 1 2 1 2 1: 1s at 1,3,5 => distances 1; 2s at 2,4 => distance 1. So min=1. Could we get distance 2? Condition: (3-1)(2+1)+1 =23+1=7>5, no. So answer 1.
Now, n=5, values: [1,1,1,1,2] (four 1s, one 2). max_freq=4, cnt_max=1. formula: (5-1)//(4-1)-1 =4//3-1=1-1=0. So answer 0. Indeed, with four 1s and one other, you can't avoid adjacent 1s. So min distance 0.
Now, n=8, values: [1,1,1,1,2,2,3,3] (four 1s, two 2s, two 3s). max_freq=4, cnt_max=1. formula: (8-1)//(4-1)-1 =7//3-1=2-1=1. So answer 1. Can we achieve min distance 1? Need 1s spaced at least 1 apart. With 4 ones and 4 others, we can interleave: 1 2 1 3 1 2 1 3 -> 1s at 1,3,5,7: distances 1; 2s at 2,6: distance 3; 3s at 4,8: distance 3. So min=1. Could we get distance 2? Condition: (4-1)(2+1)+1 =33+1=10>8, no. So answer 1.
All good.
Thus solution is correct.
Now, finalize.
One more nuance: Some participants might have used binary search; but direct formula is simpler.
Now, let's consider the note: For the first bag they gave order 1,6,4,7,1,6,4 which yields min distance 3. So output 3.
Everything matches.
Thus final answer.
Now, writing final response with explanation.
But the user might want the solution approach. In the answer, we can describe the reasoning.
However, the platform expects just code? The problem is from a contest; the user might be expecting a solution. Since they said "Pinkie Pie has bought a bag...", it's a problem statement. They want code. So we output code.
But the instruction: "You are ChatGPT, a large language model... write a solution in python." So we need to produce final answer with code. Also maybe include explanation? Usually we provide solution code and brief explanation. In this environment, we need to output final answer: the code.
Thus I'll output the code.
But ensure we handle input correctly: Since we used sys.stdin.buffer.read().split(), it's efficient.
Now, final code.
One more check: In Python, if we use from collections import Counter, it's fine. But we can also use list for frequencies to avoid import. However, Counter is simpler and within constraints.
Now, we can also compute cnt_max by: cnt_max = list(freq.values()).count(max_freq). That is fine.
Now, finalize.
Edge Cases: max_freq - 1 = 0 if max_freq=1, but that doesn't happen.
Now, produce final answer.
We'll include solve() function and main guard.
Now, final output. ```python
import sys
from collections import Counter
def solve() -> None:
data = sys.stdin.buffer.read().split()
t = int(data[0])
idx = 1
out_lines = []
for _ in range(t):
n = int(data[idx]); idx += 1
arr = list(map(int, data[idx:idx + n])); idx += n
code
freq = Counter(arr)
max_freq = max(freq.values())
cnt_max = sum(1 for v in freq.values() if v == max_freq)
# answer = (n - cnt_max) // (max_freq - 1) - 1
ans = (n - cnt_max) // (max_freq - 1) - 1
out_lines.append(str(ans))
sys.stdout.write("\n".join(out_lines))