#C1903. Reverse Segment to Sort

    ID: 45160 Type: Default 1000ms 256MiB

Reverse Segment to Sort

Reverse Segment to Sort

Given an array of integers \(a_1, a_2, \dots, a_n\), determine whether it is possible to sort the entire array in non-decreasing order by reversing exactly one contiguous segment. In other words, find indices \(l\) and \(r\) (with \(1 \leq l < r \leq n\)) such that if the subarray \(a_l, a_{l+1}, \dots, a_r\) is reversed, the resulting array becomes sorted. If such a segment exists, output its 1-indexed boundaries; otherwise, output -1.

If the array is already sorted, the answer is -1. Use the unique segment defined by the leftmost deviation from the sorted order.

inputFormat

The input begins with an integer \(T\) denoting the number of test cases.

  • For each test case:
    • The first line contains an integer \(N\), the number of elements in the array.
    • The second line contains \(N\) space-separated integers \(a_1, a_2, \dots, a_n\).

outputFormat

For each test case, print on a new line the result. If it is possible to sort the array by reversing one segment, print two integers \(l\) and \(r\) (1-indexed) representing the boundaries of the segment. Otherwise, print -1.

## sample
3
4
1 3 2 4
5
1 5 4 3 2
3
3 2 1
2 3

2 5 1 3

</p>