Permutation Sort (Codeforces 1525B)
Tags: Greedy, constructive algorithms

You are given a permutation $$$a$$$ consisting of $$$n$$$ numbers $$$1$$$, $$$2$$$, ..., $$$n$$$ (a permutation is an array in which each element from $$$1$$$ to $$$n$$$ occurs exactly once).You can perform the following operation: choose some subarray (contiguous subsegment) of $$$a$$$ and rearrange the elements in it in any way you want. But this operation cannot be applied to the whole array.For example, if $$$a = [2, 1, 4, 5, 3]$$$ and we want to apply the operation to the subarray $$$a[2, 4]$$$ (the subarray containing all elements from the $$$2$$$-nd to the $$$4$$$-th), then after the operation, the array can become $$$a = [2, 5, 1, 4, 3]$$$ or, for example, $$$a = [2, 1, 5, 4, 3]$$$.Your task is to calculate the minimum number of operations described above to sort the permutation $$$a$$$ in ascending order.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases.The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$) — the number of elements in the permutation.The second line of the test case contains $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ — the given permutation $$$a$$$.

Output: For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.

Note: In the explanations, $$$a[i, j]$$$ defines the subarray of $$$a$$$ that starts from the $$$i$$$-th element and ends with the $$$j$$$-th element.In the first test case of the example, you can select the subarray $$$a[2, 3]$$$ and swap the elements in it.In the second test case of the example, the permutation is already sorted, so you don't need to apply any operations.In the third test case of the example, you can select the subarray $$$a[3, 5]$$$ and reorder the elements in it so $$$a$$$ becomes $$$[2, 1, 3, 4, 5]$$$, and then select the subarray $$$a[1, 2]$$$ and swap the elements in it, so $$$a$$$ becomes $$$[1, 2, 3, 4, 5]$$$.

Sample Inputs:
3
4
1 3 2 4
3
1 2 3
5
2 1 4 5 3

Sample Outputs:
1
0
2

Solution (Python):
aa = []
for i in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    ll = l.copy()
    ll.sort()
    if l[-1] == 1 and l[0] == n:
        aa.append(3)
    elif l == ll:
        aa.append(0)
    elif l[-1] == n or l[0] == 1:
        aa.append(1)
    else:
        aa.append(2)
for i in aa:
    print(i)

Solution (Python):
def min_operations_to_sort(t, test_cases):
    results = []
    for (n, a) in test_cases:
        if a == sorted(a):
            results.append(0)
            continue
        if a[0] == 1 or a[-1] == n:
            results.append(1)
        elif a[0] == n and a[-1] == 1:
            results.append(3)
        else:
            results.append(2)
    return results
t = int(input().strip())
test_cases = []
for _ in range(t):
    n = int(input().strip())
    a = list(map(int, input().strip().split()))
    test_cases.append((n, a))
results = min_operations_to_sort(t, test_cases)
for result in results:
    print(result)

Solution (Python):
for t_case in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    if a == list(range(1, n + 1)):
        print(0)
    elif a[0] == 1 or a[-1] == n:
        print(1)
    elif a[0] == n and a[-1] == 1:
        print(3)
    else:
        print(2)