Prof. Slim (Codeforces 1670A)
Tags: Greedy, sortings

One day Prof. Slim decided to leave the kingdom of the GUC to join the kingdom of the GIU. He was given an easy online assessment to solve before joining the GIU. Citizens of the GUC were happy sad to see the prof leaving, so they decided to hack into the system and change the online assessment into a harder one so that he stays at the GUC. After a long argument, they decided to change it into the following problem.Given an array of $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$, where $$$a_{i} \neq 0$$$, check if you can make this array sorted by using the following operation any number of times (possibly zero). An array is sorted if its elements are arranged in a non-decreasing order.  select two indices $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$) such that $$$a_i$$$ and $$$a_j$$$ have different signs. In other words, one must be positive and one must be negative.  swap the signs of $$$a_{i}$$$ and $$$a_{j}$$$. For example if you select $$$a_i=3$$$ and $$$a_j=-2$$$, then they will change to $$$a_i=-3$$$ and $$$a_j=2$$$. Prof. Slim saw that the problem is still too easy and isn't worth his time, so he decided to give it to you to solve.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^{5}$$$) — the length of the array $$$a$$$.The next line contain $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_{i} \le 10^9$$$, $$$a_{i} \neq 0$$$) separated by spaces describing elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

Output: For each test case, print "YES" if the array can be sorted in the non-decreasing order, otherwise print "NO". You can print each letter in any case (upper or lower).

Note: In the first test case, there is no way to make the array sorted using the operation any number of times.In the second test case, the array is already sorted.In the third test case, we can swap the sign of the $$$1$$$-st element with the sign of the $$$5$$$-th element, and the sign of the $$$3$$$-rd element with the sign of the $$$6$$$-th element, this way the array will be sorted.In the fourth test case, there is no way to make the array sorted using the operation any number of times.

Sample Inputs:
4
7
7 3 2 -11 -13 -17 -23
6
4 10 25 47 71 96
6
71 -35 7 -4 -11 -25
6
-45 9 -48 -67 -55 7

Sample Outputs:
NO
YES
YES
NO

Solution (Python):
result = []
T = int(input())
for i in range(T):
    n = int(input())
    a = list(map(int, input().split()))

    def checkSorted():
        isSorted = True
        for i in range(len(a) - 1):
            if a[i] > a[i + 1]:
                isSorted = False
        return isSorted
    negative = 0
    for i in range(len(a)):
        if a[i] < 0:
            negative += 1
    out1 = True
    for i in range(negative - 1):
        if abs(a[i]) < abs(a[i + 1]):
            out1 = False
    out2 = True
    for i in range(negative, len(a) - 1):
        if abs(a[i]) > abs(a[i + 1]):
            out2 = False
    if out1 & out2 or checkSorted():
        result.append('YES')
    else:
        result.append('NO')
for i in range(len(result)):
    print(result[i])

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n = int(input())
    w = input()[:-1]
    c = w.count('-')
    w = list(map(int, w.replace('-', '').split()))
    i = 0
    while c:
        w[i] = -w[i]
        c -= 1
        i += 1
    if w == sorted(w):
        print('YES')
    else:
        print('NO')

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    flag = False
    (left, right) = (0, n - 1)
    while left < right:
        if arr[left] > 0:
            while arr[right] > 0 and left < right:
                right -= 1
            if left < right:
                arr[left] *= -1
                arr[right] *= -1
            else:
                break
        left += 1
    for i in range(n - 1):
        if arr[i] > arr[i + 1]:
            print('NO')
            break
    else:
        print('YES')