We need to parse problem: Given an array A of N integers. Kevin likes array if all neighboring elements are different, i.e., no two adjacent elements are equal. We can reverse any contiguous subarray any number of times. We want minimum number of reversals to achieve an array with no adjacent equal elements. If impossible, output -1.
Observations: Reversals are operations that can reorder elements in a flexible way? Actually reversing a contiguous subarray is a specific operation that changes the order within that subarray. But we can do many reversals, which collectively can permute the array? Not exactly: The set of permutations generated by reversals (reversals of contiguous segments) is the whole symmetric group? Actually reversals generate the symmetric group (i.e., any permutation can be achieved by a series of reversals). Because reversals are like adjacent transpositions? Actually adjacent transpositions are a special case of reversals (reversing a subarray of length 2 is swapping adjacent elements). So yes, any permutation can be achieved by a series of reversals. However, the number of operations matters, and we want minimal number.
But we need to find minimal number of reversals to get an array with no adjacent equal elements. Equivalent: Starting from given permutation (order of elements), we can apply reversals to reorder elements arbitrarily, but the cost is number of reversals. So we need to find a permutation of the multiset of values that satisfies adjacent distinct condition, and then find the minimal number of reversals to achieve that permutation from the original. That is akin to sorting by reversals? But we are not sorting to a specific target order; we can choose any target order that satisfies condition. And we want to minimize reversals to transform the initial order into some target order. Since reversals are symmetric, the minimal number of reversals to transform permutation P to permutation Q is the same as the reversal distance between two permutations, which is known to be hard in general (sorting by reversals problem is NP-hard for unsigned permutations? Actually sorting unsigned permutations by reversals is NP-hard, but for signed permutations it's polynomial. However, here we have multiset with duplicates, which makes it more complicated.)
But maybe there is a simpler interpretation: Since reversals can be applied arbitrarily, perhaps we can think in terms of the array as a sequence of values. The operation flips a segment. Could there be a combinatorial property that determines feasibility? And maybe the minimum number of reversals is limited to small numbers (like 0,1,2) or something? Let's analyze.
First, check feasibility: Is it always possible to achieve an array with no adjacent equal elements, given we can reorder arbitrarily? Not always; it depends on frequencies. For a multiset of values, we can arrange them in a sequence with no adjacent equal if and only if the maximum frequency of any value <= ceil(N/2) (pigeonhole principle for arrangements of a multiset with no identical adjacent). Actually for a sequence with no equal neighbors, necessary and sufficient condition is that no value appears more than (N+1)/2 (i.e., floor((N+1)/2) or ceil(N/2)?). Let's formalize: For N elements, if a value appears m times, then in any arrangement without adjacent equal, you need to place other elements between them, so m <= N - m + 1? Actually consider placing m items such that no two are adjacent. This is like having N positions, m items of same color, they must be separated by at least one other. The maximum m possible is ceil(N/2) because you can place at most one per every two positions: pattern X _ X _ ... So condition is m <= (N+1)//2 (integer). For N even, max m <= N/2; for N odd, max m <= (N+1)/2. So condition: max frequency <= (N+1)//2.
Thus if the frequency condition fails, it's impossible => output -1.
If possible, we need minimum number of reversals to achieve such an arrangement.
Observation: Since reversals can simulate any permutation, the minimal reversal distance from initial permutation to any target permutation that satisfies condition is essentially the minimal number of reversals needed to sort (or rearrange) to that target.
But maybe we can always achieve with at most 2 reversals? Sample required 2. Could there be cases requiring more? Possibly.
Let's think: Reversal is a powerful operation. Perhaps we can always achieve the desired arrangement with at most 2 reversals? Let's test small examples.
Suppose N=5, A = [1,1,2,2,3] (frequencies: 1 appears twice, 2 appears twice, 3 once => max=2 <=3, feasible). Can we rearrange to a good sequence with 0 reversals? The original may already be good? Check: 1-1 adjacent equal, not good. So need to change. Can we achieve with 1 reversal? Reversing a subarray could rearrange to avoid adjacent equal. For instance, original: 1,1,2,2,3. If we reverse whole array, get 3,2,2,1,1 - still adjacents equal. Reverse segment [2,4] (positions 2-4): becomes [1,2,2,1,3]? Actually reversing indices 2..4 (1-indexed) original: index1=1,2=1,3=2,4=2,5=3. Reverse subarray 2-4 gives [1,2,2,1,3] still adjacent equal? Check: 1-2 OK, 2-2 equal, not good. Reverse [1,3] => [2,1,1,2,3] has 1-1 equal. Reverse [2,5] => [1,3,2,2,1]? Actually positions 2..5: [1,2,2,3] reversed => [3,2,2,1], so array becomes [1,3,2,2,1] -> adjacent: 1-3 ok, 3-2 ok, 2-2 equal. Reverse [1,4] => [2,2,1,1,3] -> 2-2 equal. Reverse [3,5] => [1,1,3,2,2] -> 1-1 equal. Reverse [1,2] => swap first two -> [1,1,...] same. So maybe 1 reversal is insufficient? Let's systematically search small N. But maybe it's possible with 1? Consider reversing [2,3] -> indices 2,3: [1,2,1,2,3]? Actually reversing subarray of length 2 at positions 2-3: original: 1,1,2,2,3 -> after reverse positions 2 and 3: array becomes [1,2,1,2,3] . Adjacent: 1-2 OK, 2-1 OK, 1-2 OK, 2-3 OK. That works! Indeed [1,2,1,2,3] is good. So 1 reversal suffices. So maybe sometimes 1.
Now consider a case where maybe need 2? Sample needed 2. Could need more than 2? Let's try to find a counterexample.
We need to think about the structure: Reversals can be thought of as cutting the array into three parts: prefix, reversed segment, suffix. This can reorder but not arbitrarily; however with multiple reversals we can achieve any permutation.
But the minimum number of reversals to achieve a target permutation from a given permutation is the reversal distance. For unsigned permutations, it's known to be NP-hard in general, but we can have up to N=1e5, so the problem must have a simpler answer, maybe always 0,1,2 or -1.
Let's read the problem statement again: "Kevin can reverse any contiguous subarray of his array any number of times." There is no restriction on number of reversals. They ask minimum number necessary. Constraints: N up to 1e5, which suggests O(N) or O(N log N) solution. So answer likely small constant (0,1,2) or maybe determined by some measure like number of "bad adjacencies" or something.
Let's attempt to derive.
First, we need to check if array already liked => 0 reversals.
If not, maybe we can always fix with 1 reversal? Not always; sample required 2. Could there be cases where need more than 2? Perhaps not. Let's try to construct a case that might require 3.
Suppose we have many elements. The ability to reorder by one reversal is limited: it essentially takes a contiguous segment and reverses its order. That can only change adjacency relationships within and at boundaries. Could we fix all adjacent equal pairs with one reversal? Possibly not if there are multiple problematic spots that cannot be resolved simultaneously by flipping one segment.
But maybe we can always achieve with at most 2 reversals, because we can reorder the array into any desired permutation with at most 2 reversals? Actually, can we get any permutation with 2 reversals? Not exactly; with two reversals we can achieve many permutations, but not all. For example, consider permutation from 1 2 3 4 5 to 2 4 1 5 3. Is that achievable with 2 reversals? Possibly not. The set of permutations achievable with at most k reversals is known as the reversal diameter. The reversal distance can be as high as N-1 for some permutations (since it's the "sorting by reversals" problem, the reversal distance for unsigned permutations can be as high as N-1 for the reverse permutation? Actually sorting by reversals: worst-case distance for unsigned is N-1? I recall the reversal diameter for unsigned permutations is N-1 (for n>0). For example, to reverse the whole array, you can do it in 1 reversal (just reverse the whole array). Actually to transform [1,2,3,4,5] to [5,4,3,2,1], one reversal (reverse whole) works. So the distance is at most N-1? Actually known that any permutation can be sorted with at most N-1 reversals (simple algorithm: place element 1 in place using reversal, etc.) The exact reversal distance (unsigned) is NP-hard to compute, but the diameter is N-1? I'm not fully sure; but there are permutations that require N-1 reversals? I think the reversal diameter for unsigned permutations is n-1. For signed permutations it's n. For unsigned, the greedy algorithm can sort in at most n-1 reversals. There exist permutations requiring n-1 reversals? Possibly like [2,3,4,...,n,1] might require multiple. But known that the reversal distance for unsigned permutations is at most n-1 (since we can always bring the largest element to end, etc.). So worst-case distance can be linear. So if we need to achieve a specific target permutation, we might need many reversals. But we are allowed to choose any target permutation that satisfies condition. So we can pick a target that is "close" to the original in reversal distance.
Thus the problem reduces to: Given a sequence, we want to find a permutation (of the multiset) with no adjacent equal that minimizes the reversal distance from the original sequence. The answer is that minimum reversal distance.
Given that N is up to 1e5, maybe we can compute this distance efficiently if it's always small. But could be large. However, maybe we can always achieve a good arrangement by reordering only a few elements via reversals, and the condition about adjacent equal can be satisfied by just breaking local equalities without a full reorder.
Let's explore deeper.
Observation: A reversal operation effectively allows us to take a contiguous block and flip its order. This can be used to bring elements from one side to the other, etc.
We can think about the positions of elements that are "bad": adjacent equal pairs. Let's denote each index i (1<=i<N) where A[i]==A[i+1] as a bad edge. We want to eliminate all bad edges.
If the array already satisfies condition, answer 0.
If not, can we always fix all bad edges with exactly 1 reversal? Possibly if there is a contiguous segment such that reversing it makes all adjacents distinct.
When we reverse a subarray [L,R], the order inside changes, and the adjacencies at boundaries: between L-1 and L (new L is old R), and between R and R+1 (new R is old L). Also all edges inside the segment get reversed: originally edges (i,i+1) for i in [L,R-1] become edges (j+1,j) after reversal? Actually careful: after reversal, the sequence within [L,R] is reversed. So the adjacency pairs inside become between elements that were previously not necessarily adjacent. The internal adjacency structure is completely changed. So a reversal can drastically alter adjacencies.
Maybe we can characterize when 1 reversal suffices.
Consider we have an array. We want to find a reversal that yields no equal adjacent. This is like checking if there exists a pair (L,R) such that after reversal, the array has no equal neighbors. Could we check this in O(N) or O(N^2)? N up to 1e5, so O(N^2) is too much. But if answer is only 0,1,2, we could perhaps detect if 1 reversal possible efficiently.
But need to ensure we have algorithm to compute minimal number, which might be 0,1,2 or maybe more. Could it be more? Let's try to construct examples requiring more than 2.
We need to think about the operation: reversing a subarray is equivalent to taking that segment and flipping it. Could we achieve any permutation with 2 reversals? Not exactly, but perhaps any arrangement satisfying the distinct adjacent condition can be obtained from the original with at most 2 reversals? That would be surprising.
Let's test with N=6, A = [1,1,1,2,2,2]? But frequencies: 1 appears 3, 2 appears 3, N=6, max frequency=3 <=3? For N=6, (N+1)//2 = 3. So feasible. But original has many adjacent equals. Can we achieve a good arrangement with 2 reversals? A good arrangement could be 1,2,1,2,1,2 or 2,1,2,1,2,1. Starting from [1,1,1,2,2,2], can we get to e.g., 1,2,1,2,1,2 with 2 reversals? Let's attempt: One reversal: reverse whole array -> [2,2,2,1,1,1] still bad. Another reversal: maybe reverse segment [2,5]? Hard to see. Could 1 reversal work? Check if there is any reversal that yields all distinct adjacents. Try reversing subarray [2,5] (indices 2-5): original: 1,1,1,2,2,2. Reversed segment: positions 2..5 become reversed of [1,2,2,1]? Actually original segment 2-5: [1,1,2,2]? Wait: indices: 1:1, 2:1, 3:1, 4:2, 5:2, 6:2. So segment 2-5: indices 2:1, 3:1, 4:2, 5:2 -> [1,1,2,2]. Reverse => [2,2,1,1]. New array: index1=1, then reversed segment: indices2=2,3=2,4=1,5=1, index6=2. So array: [1,2,2,1,1,2]. Adjacencies: 1-2 ok, 2-2 equal -> bad. Try other reversals: maybe reverse [1,4] => segment [1,1,1,2] reversed => [2,1,1,1] -> array [2,1,1,1,2,2] -> 1-1 equal. Reverse [3,6] => [1,1,2,2,2,1]? Actually original 3-6: [1,2,2,2] reversed => [2,2,2,1] -> array [1,1,2,2,2,1]? Wait indices: 1:1,2:1, then reversed seg at positions 3-6 becomes [2,2,2,1]? Actually careful: start: [1,1,1,2,2,2]. Reverse [3,6] means indices 3,4,5,6: values [1,2,2,2] reversed => [2,2,2,1] -> array becomes [1,1,2,2,2,1]. Adjacencies: 1-1 equal at 1-2? indices1-2: 1 and 1 equal -> bad. So none works with 1.
Could 2 reversals achieve? Let's try to find a sequence of two reversals that transforms [1,1,1,2,2,2] to a good arrangement. Since we can use two reversals, we might be able to simulate a simple swap or more complex reorder.
One idea: first reversal to bring some 2's to front, second to interleave. For example: reverse whole array -> [2,2,2,1,1,1]. Now we have three 2's then three 1's. Still bad. Then reverse a middle segment to interleave? Try reverse [2,5] in the new array: [2,2,2,1,1,1] reverse indices 2-5: segment [2,2,1,1] reversed => [1,1,2,2] -> array: [2,1,1,2,2,1]? Actually index1=2, then indices2-5 become [1,1,2,2], index6=1 => [2,1,1,2,2,1]. Adjacent: 2-1 ok, 1-1 equal -> bad.
Try other combo: first reverse [1,3] => [1,1,1,2,2,2]? Actually reverse first three: [1,1,1] reversed => [1,1,1] same, so no change. Not helpful.
Maybe reverse [2,4]: segment [1,1,2] reversed => [2,1,1] -> array: [1,2,1,1,2,2]? Actually original: 1,1,1,2,2,2. Reverse indices 2-4: values at 2,3,4: 1,1,2 => reversed => 2,1,1 => array: index1=1, index2=2, index3=1, index4=1, index5=2, index6=2 -> [1,2,1,1,2,2] -> 1-1 equal at 3-4. Then second reversal could fix that. For example, now we have [1,2,1,1,2,2]. If we swap positions 4 and 5 (reverse length 2 at [4,5]) => [1,2,1,2,1,2] -> good! So that's two reversals: first reverse [2,4] -> yields [1,2,1,1,2,2]; second reverse [4,5] -> yields [1,2,1,2,1,2]. So 2 reversals works.
Thus the example we constructed needed at least 2, and we found a 2-step solution. So maybe 2 is sufficient for all feasible cases? But is there a case that requires 3? Let's test more.
Consider N=5, A=[1,1,1,2,2]? Frequencies: 1 appears 3, 2 appears 2, N=5 => max allowed =3, so feasible. Original: [1,1,1,2,2] has adjacent equals. Can we do in 1 reversal? Try possibilities: reverse [2,4] -> segment [1,1,2] reversed => [2,1,1] -> array: [1,2,1,1,2] -> adjacent: 1-2 ok, 2-1 ok, 1-1 equal at indices 3-4. Not good. Reverse [3,5] -> segment [1,2,2] reversed => [2,2,1] -> array: [1,1,2,2,1] -> 1-1 equal at 1-2, 2-2 at 3-4. Reverse [1,3] -> [1,1,1] reversed => [1,1,1] unchanged, same. Reverse [1,4] -> [1,1,1,2] reversed => [2,1,1,1] -> array: [2,1,1,1,2] -> 1-1 at 2-3 or 3-4. Reverse [2,5] -> [1,1,2,2] reversed => [2,2,1,1] -> array: [1,2,2,1,1] -> 2-2 at 2-3, 1-1 at 4-5. Reverse [1,5] whole array -> [2,2,1,1,1] -> 2-2 at 1-2, 1-1 at 3-4 or 4-5. So 1 reversal seems insufficient. So at least 2 needed.
Can we do with 2? Let's try: Start [1,1,1,2,2]. Want to achieve e.g., [1,2,1,2,1] or [2,1,2,1,1]? The latter has adjacent 1-1 at end? Actually [2,1,2,1,1] has 1-1 at end. So we need arrangement with no equal adjacent: with frequencies 3 ones and 2 twos, the only patterns are 1,2,1,2,1 or 2,1,2,1,1? Wait 2,1,2,1,1 has 1-1 at positions 4-5. So only pattern is 1,2,1,2,1 (or its reverse 1,2,1,2,1 same). Also 2,1,2,1,? Actually we need to place three ones and two twos, so must start and end with 1: 1_1_1 with twos in between: 1,2,1,2,1 is the only pattern (up to symmetry). So target is 1,2,1,2,1.
Can we get that with 2 reversals from original? Let's attempt: first reversal: maybe reverse [2,5] -> [1,2,2,1,1]? Actually we tried that yields [1,2,2,1,1]. Then second reversal: reverse [3,4]? [1,2,2,1,1] reverse indices 3-4: segment [2,1] -> [1,2] -> array [1,2,1,2,1] good! So indeed: first reverse [2,5] from original => [1,2,2,1,1]; second reverse [3,4] => [1,2,1,2,1]. So 2 works.
Thus far, examples that are not already good need at most 2.
But consider more complex: many different values? If the array already satisfies condition? 0.
If not, maybe always 1 or 2. Let's try to see if there is a case where 1 reversal cannot fix, but 2 can. Possibly always 2 suffices if feasible, unless already good. But sample required 2, and they said "There's no way to create an array which Kevin likes using only one reversal." So they gave a case where 1 is impossible but 2 works. Could there be cases where 2 is impossible but 3 needed? Let's think.
Maybe the problem reduces to checking for 0, then for 1, else 2, else -1? That would be simple if we can prove that if the frequency condition holds, it's always possible with at most 2 reversals. Is that always true? Let's test some random examples mentally.
Take N=7, values: three 1's, two 2's, two 3's. Feasible since max 3 <=4? (N+1)//2 =4. Could original arrangement be such that one reversal cannot fix? Possibly. But we need to see if 2 reversals always enough to achieve any valid arrangement? Or maybe there are cases where we need more than 2 because the array might be something like [1,1,2,2,3,3,4]? Frequencies: 1,2,3 each appear 2, 4 appears 1. N=7, max 2 <=4. Could we achieve a good arrangement with 2 reversals? Likely yes. But can we prove?
Idea: With two reversals, we can effectively perform a "cut and paste" of a contiguous block to a new location? Actually any permutation can be expressed as at most 2 reversals if we allow reversal of arbitrary segments? No, two reversals can't generate arbitrary permutations. For instance, to swap two non-adjacent elements while keeping others fixed, you might need more than 2 reversals? Let's check: To transform [a,b,c,d] to [a,c,b,d] (swap b and c). Can we do with two reversals? Option: Reverse [2,3] -> [a,c,b,d] in one reversal! Because reversing a subarray of length 2 swaps adjacent elements. So swapping adjacent elements is one reversal. For non-adjacent, e.g., [a,b,c,d] to [c,b,a,d] (move a and c). Can we do with two reversals? Perhaps: reverse [1,3] -> [c,b,a,d] done in one reversal. So many transpositions are one reversal. Actually any reversal that takes a contiguous segment and reverses it can reorder arbitrarily? Not exactly; but note that a single reversal can perform a cyclic shift? For example, to rotate left by 1: [a,b,c,d] -> [b,c,d,a]. Can we do with one reversal? Reverse whole array: [d,c,b,a] not that. Reverse [1,4] -> reversed whole, not rotate. To rotate, you might need two reversals: reverse two parts. Indeed, rotation can be done by two reversals: reverse [1,k] and reverse [k+1,N] and then reverse whole? Actually standard rotation using three reversals. But we can do rotation with two? Possibly if we reverse a segment that is not contiguous? No. So rotation may require two reversals? Actually to rotate left by 1: [a,b,c,d] -> [b,c,d,a]. Can we do with one reversal? Suppose we reverse [1,4] -> [d,c,b,a]. Not good. Reverse [1,3] -> [c,b,a,d]. Not good. So maybe one reversal cannot achieve rotation. Two reversals: first reverse [1,3] -> [c,b,a,d]; then reverse [1,4]? That yields [d,a,b,c] not correct. Hmm. Maybe two reversals can achieve rotation: Consider reversal of [1,2] then reversal of [3,4]? [a,b,c,d] -> after [1,2] -> [b,a,c,d]; after [3,4] -> [b,a,d,c] not. Not rotation. So rotation might require three reversals. But we don't need a specific target; we can choose any target arrangement that satisfies condition. So maybe we can pick a target that is "close" to the original, requiring at most 2 reversals. Perhaps we can always rearrange to a good sequence by at most 2 reversals because we can fix the bad adjacencies by moving at most two blocks.
Let's attempt to design a constructive proof: Given the array A, if it already has no adjacent equal, done (0). Else, there exists at least one index i with A[i]=A[i+1]. The goal is to eliminate all such equal adjacencies.
Observation: A reversal can be used to bring an element from somewhere else to break a bad adjacency. For example, if we have ...x,x... we need to insert a different element between them. That could be done by reversing a segment that includes one of the x's and some other element.
Consider the simplest case: if there is any element y different from x that appears somewhere, we could try to bring it between the two x's via a reversal. For instance, if we have ... x, x, ... and somewhere later a y, we could reverse the segment from the second x to the y, which would place y next to the first x? Let's simulate: A = [..., x, x, ..., y, ...]. If we reverse subarray from the second x (position i+1) to the position of y (j), then after reversal, the element at position i+1 becomes y (since the segment reversed, the last element becomes first). So we get x, y, ... and the element after y becomes the old element before y? That could break the adjacency. However, this may create other bad adjacencies.
Maybe we can always fix all bad adjacencies with at most two reversals because we can split the array into two parts and rearrange them to interleave.
Another thought: The operation of reversing a subarray essentially allows us to change the order of a contiguous block arbitrarily to its reverse. So we can flip the order of any contiguous segment. With two reversals, we can effectively take any contiguous segment and move it to another location? For example, to move a segment to the front: we can reverse the segment, then reverse a larger segment. There's known fact: any permutation can be expressed as at most two reversals? That's false. But perhaps for the purpose of achieving no equal adjacent, we don't need arbitrary permutation; we only need to "fix" bad pairs, which are local.
Let's test a tricky example: N=8, A = [1,1,1,1,2,2,2,2]? But frequency: 4 ones, 4 twos, max 4, N=8 => allowed max is 4? (N+1)//2=4, so just feasible. But need arrangement: 1,2,1,2,1,2,1,2 or 2,1,2,1,2,1,2,1. Original has many adjacent equals. Can we achieve with 2 reversals? Let's try to see if 2 suffices.
Original: [1,1,1,1,2,2,2,2]. Target: [1,2,1,2,1,2,1,2]. Is there a 2-reversal sequence? Possibly: First reversal to split blocks? Maybe reverse the middle segment to interleave? For instance, reverse [3,6] (positions 3-6): segment = [1,1,2,2] reversed => [2,2,1,1] -> array: [1,1,2,2,1,1,2,2]? Actually careful: indices 1:1,2:1,3:1,4:1,5:2,6:2,7:2,8:2. Reverse indices 3-6: values 3:1,4:1,5:2,6:2 -> reversed => [2,2,1,1]. New array: indices:1=1,2=1,3=2,4=2,5=1,6=1,7=2,8=2. That gives [1,1,2,2,1,1,2,2]. Still many adj equal. Then second reversal could be e.g., reverse [2,7]? That seems messy.
Maybe we can do: First reverse whole array -> [2,2,2,2,1,1,1,1]. Still bad. Then reverse [2,7]? Not sure.
Maybe two reversals are insufficient for this case? Let's try to systematically search small N via reasoning or maybe brute force for N=8? Not easy manually. But we can attempt to see if there is known result: The problem likely is from a contest, maybe answer is always 0,1,2,-1. Could there be cases requiring more than 2? Let's search memory: I recall a Codeforces problem about "minimum number of reversals to make array good" where answer is 0,1,2,-1. Actually I recall a problem "Kevin and Array" maybe from Hackerearth? There is a known problem "Kevin and Array Reversal" where answer is either -1,0,1,2. And sample shows 2. I'm not sure but likely that's the case.
Let's try to prove or find counterexample.
We can think in terms of the pattern of equal blocks. Suppose the array consists of blocks of consecutive equal elements. For example, [1,1,2,2,3,3] etc. A single reversal can merge or split blocks. We want to end up with no two consecutive equal, i.e., all blocks of size 1. So we need to break all blocks of size >=2.
Observation: If there is more than one value with frequency > 1, we might need to interleave.
Consider an array with two blocks: e.g., [X...X][Y...Y] where all X's then all Y's. This is like all ones then all twos. Example: [1,1,1,2,2,2] we already solved with 2 reversals. Could we do it with 1? Possibly if we reverse a segment that brings some Y into X block and vice versa. For the all-ones then all-twos case, can we achieve a good array with one reversal? The target must interleave them, like 1,2,1,2,... . Starting from [1,1,1,2,2,2], is there a reversal that directly produces an interleaving? Let's check all possible reversals for N=6 case earlier. We attempted and found none? Let's test systematically: N=6, array [1,1,1,2,2,2]. We need a permutation with no adjacent equal. The only valid patterns are 1,2,1,2,1,2 and 2,1,2,1,2,1. Can a single reversal produce exactly one of these? Let's examine all possible reversals (there are O(N^2) but we can reason). The reversal operation will reverse a contiguous segment. The resulting sequence will have the structure: first part (up to L-1) unchanged, then reversed segment, then suffix unchanged. To get interleaving 1,2,1,2,1,2, the sequence must alternate. The original has a block of 1s then block of 2s. To achieve alternation, we need to interleave. A single reversal can only reverse a segment, not arbitrarily permute. It can change the order within the segment to be reverse, but it can't change the relative order of elements outside the segment. So, if we pick a segment that includes part of both blocks, reversing will produce a segment that starts with some 2s (if we include the transition) and ends with some 1s. This might create alternation? For example, if we reverse from position i (in the 1s block) to j (in the 2s block), after reversal, the segment will have the 2s first then 1s. Then the prefix before i is all 1s, the segment starts with 2s, so after prefix (which ends with a 1), we have a 2 -> good. Then inside the segment, after 2s we have 1s, so alternation could hold. But also after the segment, the suffix starts with whatever was after j originally, which is part of the 2s block if j is not the end? Actually after j (original), there are more 2s (if any). After reversal, the segment ends with 1s, and then suffix begins with the original elements after j, which are 2s (since originally after the 2 block). So we may get a pattern: prefix (1s) then segment (2s...1s) then suffix (2s). So we could get something like 1...1,2,...,1,2,...,2. This could potentially alternate? Let's try concrete: N=6, original indices: 1:1,2:1,3:1,4:2,5:2,6:2. Choose L=2,R=5. Segment indices 2-5: [1,1,2,2] reversed -> [2,2,1,1]. Result: index1=1, index2=2, index3=2, index4=1, index5=1, index6=2 => [1,2,2,1,1,2] not alternating because we have 2,2 and 1,1. So fails.
What about L=3,R=6? Segment [1,2,2,2] reversed -> [2,2,2,1] -> array: [1,1,2,2,2,1] -> [1,1,...] fails. L=1,R=4? [1,1,1,2] reversed -> [2,1,1,1] -> [2,1,1,1,2,2] fails. L=2,R=6? [1,1,2,2,2] reversed -> [2,2,2,1,1] -> [1,2,2,2,1,1] fails. L=1,R=5? [1,1,1,2,2] reversed -> [2,2,1,1,1] -> [2,2,1,1,1,2] fails. L=3,R=5? [1,2,2] reversed -> [2,2,1] -> [1,1,2,2,1,2]? Actually indices: 1:1,2:1, segment 3-5: [1,2,2] -> reversed [2,2,1]; resulting: [1,1,2,2,1,2] -> [1,1,...] fails. L=2,R=4? [1,1,2] reversed -> [2,1,1] -> [1,2,1,1,2,2] -> fails because 1,1. L=4,R=6? [2,2,2] reversed same -> no change. L=1,R=6? whole reversed -> [2,2,2,1,1,1] fails. So no single reversal yields alternating pattern. Could there be a different alternating pattern that is not perfect interleaving? With 3 ones and 3 twos, any arrangement with no adjacent equal must be perfect alternating starting with either 1 or 2. So indeed 1 reversal insufficient.
Thus for the case of two blocks (all same then all same of another value) with equal counts > 1, 1 reversal is insufficient, but 2 suffice. Good.
Now consider three blocks: e.g., [1,1,2,2,3,3]? N=6, values: each appears 2, max=2 <=3, feasible. Original has adjacencies within blocks. Can we do in 1 reversal? Possibly? Let's test: A = [1,1,2,2,3,3]. We need arrangement with no adjacent equal. For example 1,2,1,3,2,3? But must use counts: two 1s, two 2s, two 3s. Can we get with 1 reversal? Let's try to find reversal that yields no equal neighbors. Try reversing [2,5] (indices 2-5): segment = [1,2,2,3] reversed -> [3,2,2,1] -> array: [1,3,2,2,1,3] -> adjacent: 2,2 equal. Try [3,6]: [2,2,3,3] reversed -> [3,3,2,2] -> [1,1,3,3,2,2] -> 1,1 equal and 3,3 equal. Try [1,4]: [1,1,2,2] reversed -> [2,2,1,1] -> [2,2,1,1,3,3] -> many equals. Try [2,4]: [1,2,2] -> [2,2,1] -> [1,2,2,1,3,3] -> 2,2 and 3,3. Try [3,5]: [2,2,3] -> [3,2,2] -> [1,1,3,2,2,3] -> 1,1 and 2,2. Try [4,6]: [2,3,3] -> [3,3,2] -> [1,1,2,3,3,2] -> 1,1 and 3,3. Try [1,2] swap adjacent -> [1,1,...] same. So probably 1 reversal not enough. Can we do with 2? Probably yes. Let's attempt: first reversal to create a better configuration, then second. For instance, reverse [2,5] gave [1,3,2,2,1,3] (as above). Then second reversal could reverse the middle [3,4] (2,2) -> [1,3,2,2,1,3] reversing [3,4] gives [1,3,2,2,1,3]? Actually reversing [3,4] swaps the two 2's, no change. So not that. Reverse [3,5]? [2,2,1] -> [1,2,2] -> [1,3,1,2,2,3]? Let's compute: Starting from [1,3,2,2,1,3], reverse indices 3-5 (2,2,1) -> [1,2,2]? Actually reverse(2,2,1) = (1,2,2). New array: index1=1, index2=3, index3=1, index4=2, index5=2, index6=3 -> [1,3,1,2,2,3] still 2,2. Reverse [4,5]? [2,2] -> same. So not working. Try different first reversal.
Maybe we can achieve in 2. I'd suspect it's always possible with 2. But need to be sure.
Let's try to see if there is a known result: The problem might be from Hackerearth April Circuits 2017 or something. I recall a problem "Kevin and Array" where answer is 0,1,2,-1. Let's search memory: There is a Codeforces problem "Make It Good" or something? Not sure.
Another approach: Let's think about graph representation. Since we can do any number of reversals, we could eventually achieve any permutation. But we want minimal number. Could it be that the reversal distance to any "good" permutation is at most 2? Not obviously. Consider an array that requires a major reordering, e.g., [1,1,2,1,1,2,2,2]? I need to test.
Maybe the minimal number is determined by the number of "bad segments" or something. Could it be that if there exists at least one element that appears exactly once, then 1 reversal suffices? Not sure.
Let's try to systematically think about cases where 1 reversal might work.
Condition for 1 reversal to be sufficient: There exists a subarray [L,R] such that after reversal, the array has no equal neighbors. This is a property we could test in O(N) by some smart reasoning? Possibly but if answer is always 0,1,2, we just need to check if 0, then if 1 possible, else answer 2 (provided feasible). So we don't need to compute distance exactly; we just need to decide if 1 reversal is enough.
Thus the problem reduces to: given array A, first check feasibility (maxfreq <= (N+1)//2). If not, output -1. Else, if array already good, output 0. Else, check if there exists a reversal that makes it good. If yes, output 1. Else output 2.
But is it always true that if feasible and not already good and not 1-reversal solvable, then 2 reversals always suffice? This would need proof.
Before concluding, let's test more complex cases to see if 2 reversals always work.
Consider N=8, frequencies: ones:4, twos:4 as earlier. We need to achieve an alternating pattern. Can we achieve alternating pattern with 2 reversals from [1,1,1,1,2,2,2,2]? Let's try to construct explicitly.
Goal: 1,2,1,2,1,2,1,2.
One idea: Use two reversals to effectively "rotate" or "swap" halves.
Observation: The target is interleaved. Starting from all 1s then all 2s, we need to bring 2s forward and 1s backward. Could we do it with two reversals: first reverse the whole array, then reverse a specific segment? Whole reversal gives [2,2,2,2,1,1,1,1]. Now we have all 2s then all 1s. Still not alternating. Then reverse the segment from 2 to 7? That yields? Let's compute: after whole reversal: indices:1:2,2:2,3:2,4:2,5:1,6:1,7:1,8:1. Reverse [2,7]: segment indices2-7: [2,2,2,1,1,1] reversed => [1,1,1,2,2,2] so new array: index1=2, then segment becomes [1,1,1,2,2,2], index8=1 => [2,1,1,1,2,2,2,1]. Not good.
What about first reverse a middle segment, then another. Let's think systematically.
We want to transform [1^4,2^4] to interleaved. Could we do with one reversal? We tested many and it's plausible none works. But can we prove? Possibly the structure after one reversal from a two-block array yields at most two alternations? But maybe there is a clever one. Let's test all possible L,R for N=8, but manually we can reason about the pattern after one reversal. Original: A = [1]*a + [2]*b with a=4,b=4. After reversing subarray from L to R, where L is within the 1-block or at boundary, and R within 2-block or at boundary. The result will have the form: prefix (1s up to L-1), then reversed segment which will contain a mix: if L in 1-block and R in 2-block, the reversed segment will be: it starts with the tail of the 2-block (since original segment ends with 2s) and ends with head of 1-block. So the array becomes: some number of 1s (prefix), then some 2s (from the tail of 2s in the reversed segment), then some 1s (from the head of 1s) and then suffix (2s). This yields pattern: 1...1, 2...2, 1...1, 2...2. So there will be at least two blocks of consecutive same values: the prefix 1s and the middle 1s might merge if prefix ends with 1 and the segment starts with 2? Actually they are separated by 2s, so there will be a block of 1s, then a block of 2s, then a block of 1s, then a block of 2s. This pattern has potential equal adjacencies at boundaries between blocks: between prefix 1s and segment's first element (2) is okay (1 vs 2). Between segment's last element (1) and suffix's first element (2) is okay? Actually suffix is 2s, so segment ends with 1, suffix starts with 2 => 1 vs 2 okay. But the internal boundaries inside the segment? The segment after reversal is: originally segment had a contiguous block of 1s at its start (if L is in 1-block) and a contiguous block of 2s at its end. After reversal, the order becomes: block of 2s (originally at end) then block of 1s (originally at start). So inside the segment, there is a transition from 2s to 1s. That's okay (2 vs 1). However, there might be multiple blocks within the segment if the segment includes the transition point? Actually the segment includes the boundary between 1-block and 2-block. So original segment is: some 1s (from the 1-block) then some 2s (from the 2-block). That's exactly two blocks. After reversal, it becomes some 2s then some 1s. So inside the segment, it's all 2s then all 1s, so there is a single boundary between 2s and 1s, which is fine. But the issue is that the prefix might be empty or not, and the suffix might be empty. The only possible equal adjacency is if there are consecutive same values within the same block. Since prefix is all 1s, if prefix is non-empty and the segment starts with 2s, that's fine. However, if the segment ends with 1s and suffix is also 1s? But suffix is all 2s, so suffix is 2s, not 1s. So the segment ends with 1s and suffix starts with 2s, okay. So it seems that after one reversal from a two-block array, we could get an array that alternates? Wait, but we saw examples where we got [1,2,2,1,1,2] which had consecutive 2s and 1s. That's because the segment contained more than one element from each block leading to blocks of size >1 inside segment. Actually in that example, prefix had one 1, segment was [2,2,1,1]? Wait earlier example had [1,2,2,1,1,2]. The blocks: prefix: [1], segment: [2,2,1,1], suffix: [2]. So we have: 1, then 2,2 (consecutive 2s), then 1,1 (consecutive 1s), then 2. So indeed the segment itself had two consecutive 2s and two consecutive 1s. So the problem is that the segment's internal blocks may have size >1, causing equal adjacencies within segment. To avoid any equal adjacent, we need each block within the segment to be of size 1. That means the number of 1s and 2s in the segment must be at most 1 each? But if we have multiple 1s, they'll be consecutive after reversal? Let's analyze.
Original segment: suppose it includes x 1s (from the start of segment) and y 2s (from the end of segment). After reversal, the segment becomes y 2s followed by x 1s. So inside the segment, we have a block of y 2s (consecutive) and then a block of x 1s. Thus if y>=2, there will be consecutive 2s inside the segment. Similarly if x>=2, consecutive 1s. So to avoid any equal adjacency inside the segment, we need max(x,y) <= 1. That means the segment must include at most 1 element from each block. But if the segment includes both blocks, then x>=1 and y>=1, but to have x<=1 and y<=1, the segment must be exactly of length 2, consisting of one 1 and one 2. Then after reversal, segment becomes [2,1]. So the array would become: prefix of 1s (maybe zero), then [2,1], then suffix of 2s. Now we need to check boundaries: prefix ends with 1 (if non-empty), segment starts with 2 => fine. Segment ends with 1, suffix starts with 2 => fine. Also inside segment: [2,1] fine. Also need to ensure that prefix and suffix themselves don't contain consecutive equal? Prefix is all 1s, so if prefix length >=2, then there will be consecutive 1s within prefix. Similarly suffix all 2s if length >=2 gives consecutive 2s. So we also need prefix length <=1 and suffix length <=1. So overall, for a two-block array, a single reversal can produce a good array only if we can choose a segment of length 2 (one 1, one 2) such that the prefix and suffix are each of length <=1. That means the whole array length N must be such that after removing that segment of two elements (one from each block), the remaining elements are at most 1 from each side? Actually prefix consists of 1s only, suffix of 2s only. To have no consecutive equal in prefix, we need at most one 1 before the segment. Similarly at most one 2 after the segment. So total ones = (prefix 1s) + (if segment includes a 1) + (maybe? segment includes exactly one 1) ; total twos = (suffix 2s) + (segment includes one 2). Since total ones = a, total twos = b. If we need prefix 1s <=1 and suffix 2s <=1, then a = prefix_1s + 1 <= 2 => a <= 2. Similarly b = suffix_2s + 1 <=2 => b <= 2. So for a two-block array to be fixable with one reversal, we need a <=2 and b <=2, i.e., each block size at most 2. But if a>=3 or b>=3, one reversal cannot fix because either prefix or suffix will have at least 2 same values, or the segment would need to include more than one from each block causing internal consecutive. But maybe there are other L,R selections where the segment includes only one block? If we reverse a segment wholly within one block, that does nothing. If we reverse a segment that includes only part of one block and part of the other, we already considered.