Domain: Algorithms & Data Structures

## Reach a Regular Bracket Sequence by Flipping Equal Adjacent Pairs

You are given a bracket string `s` of length `n` consisting only of characters `(` and `)`.

You may apply the following operations any number of times (possibly zero), in any order:

1. Choose an index `i` (`1 ≤ i < n`) such that `s[i] = s[i+1] = '('`, and replace both characters with `')'`.
2. Choose an index `i` (`1 ≤ i < n`) such that `s[i] = s[i+1] = ')'`, and replace both characters with `'('`.

For each test case, output any **regular bracket sequence** `t` that can be obtained from `s` by these operations, or output `-1` if it is impossible.

A bracket sequence is **regular** iff it is a correct bracket sequence (balanced and every prefix has at least as many `(` as `)`).

### Input
Multiple test cases:
- First line: `t` — number of test cases (`1 ≤ t ≤ 10^4`)
For each test case:
- Line 1: integer `n` (`1 ≤ n ≤ 2·10^5`)
- Line 2: string `s` of length `n`

It is guaranteed that the sum of `n` over all test cases does not exceed `2·10^5`.

### Output
For each test case, print a regular bracket sequence `t` obtainable from `s`, or `-1`.


Grading rubric:
criterion: The answer defines a transformation that flips the bracket character on every even position of the string.
weight: 10
criterion: The answer states that in the transformed string the operation can rewrite the substring \"()\" into \")(\".
weight: 10
criterion: The answer states that in the transformed string the operation can rewrite the substring \")(\" into \"()\".
weight: 10
criterion: The answer states that if n is odd then the output is −1.
weight: 10
criterion: The answer states that a necessary feasibility condition is that the number of '(' characters in the transformed string is even.
weight: 10
criterion: The answer states that the transformed operations allow reaching any permutation of the transformed string’s characters.
weight: 10
criterion: The answer states that if the transformed string contains zero '(' characters then it is impossible to obtain a regular bracket sequence.
weight: 10
criterion: The answer defines x as open/2 where open is the number of '(' characters in the transformed string.
weight: 10
criterion: The answer gives an explicit construction of t as \"()\" repeated (x−1) times, followed by \"(\", followed by \"()\" repeated (n/2−x) times, followed by \")\".
weight: 10
criterion: The answer states a per-test-case time complexity of O(n)
weight: 10

Answer:
## Expert Solution

### 1) Key transformation: flip even positions

Define a transformed string `s'` (1-indexed) by flipping the bracket on every even position:
- if `s[i] = '('` then `s'[i] = ')'` for even `i`
- if `s[i] = ')'` then `s'[i] = '('` for even `i`
- odd positions stay unchanged

This transformation is invertible (apply it again to return to the original domain).

---

### 2) Operations become adjacent swaps in the transformed string

Consider an operation on equal adjacent brackets in `s`:

- If `s[i]=s[i+1]='('`, after flipping even positions the pair becomes either `"()"` or `")("` (depending on parity of `i`).
  After the operation the pair becomes `"))"`, which flips to the opposite mixed pair.
- Similarly, `"))"` operations also flip between `"()"` and `")("`.

Therefore, in `s'` the operation is exactly:
\[
"()" \leftrightarrow ")("
\]
which is an adjacent swap of two different characters.

Hence, using these operations we can perform arbitrary adjacent swaps in `s'`, so we can reach **any permutation of `s'`**.  
Equivalently, the only invariant is the multiset of characters, i.e. the number of `'('` in `s'`.

Let:
- `open = count('(' in s')`.

Then `s` can reach a target `t` iff `s'` can be permuted into `t'` where `t'` is `t` with even positions flipped, which holds iff `t'` has exactly `open` opening brackets.

---

### 3) Characterize when a regular bracket sequence is possible

A regular bracket sequence must have even length, so if `n` is odd the answer is `-1`.

Also, for any regular bracket sequence `t`, after flipping even positions the number of `'('` in `t'` is always even (it equals `2 * (# of '(' on odd positions in t)`).
So we must have `open` even.

If `open = 0`, then `t'` has no `'('`, which is impossible for any regular bracket sequence `t` (the first character of `t` must be `'('` and position 1 is not flipped).  
So we also need `open >= 2`.

Thus feasibility conditions are:
- `n` is even
- `open` is even
- `open > 0` (equivalently `open >= 2`)

If any fails, print `-1`.

---

### 4) Construct a regular bracket sequence with a prescribed `open`

Let `open = count('(' in s')` and define:
\[
x = open/2.
\]
We construct a regular bracket sequence `t` of length `n` such that after flipping even positions, `t'` has exactly `open` opening brackets.

Construction:
- Output `"()"` repeated `(x-1)` times
- Then output `"("`
- Then output `"()"` repeated `(n/2 - x)` times
- Then output `")"`

Formally:
\[
t = \underbrace{()() \dots ()}_{x-1\ \text{times}} \; + \; ( \; + \; \underbrace{()() \dots ()}_{(n/2-x)\ \text{times}} \; + \; )
\]

This is a concatenation of regular bracket sequences, so `t` is regular.

By construction, the number of `'('` on odd positions in `t` is exactly `x`, hence `t'` has `2x = open` opening brackets.

Since `s'` can be permuted into any string with the same number of `'('`, we can permute `s'` into `t'`, and applying the inverse flip yields `t` obtainable from `s`.

Output `t`.

---

### 5) Complexity

For each test case:
- building `s'` and counting `open`: `O(n)`
- constructing `t`: `O(n)`

Total over all test cases: `O(sum n)`.