I caught the sparkle in my mind and got AC1! It is a great great experience!
So the basic Idea:permute on 3 consecutive items doesn ' t change the parity of the No. of inversions. Each permutation can is only remove 0 or 2 inversions. So we say "YES" when No. of inversion% 2 = = 0. And we use MergeSort to count it in O (NLGN).
RET =0defmerge (arr1, arr2):Globalretif notARR1:returnarr2if notARR2:returnarr1 forV2inchArr2:arr1.append (v2) I= Len (arr1)-1 while(I >0):ifArr1[i] < Arr1[i-1]: Arr1[i-1],arr1[i] = Arr1[i], arr1[i-1] I-= 1ret+ = 1Else: Break returnarr1defmergesort (arr): N=Len (arr)if(N < 2):returnarrreturnMerge (MergeSort (ARR[:N//2]), MergeSort (ARR[N//2:]))###t =int (input ()) for_inchRange (t): N=int (input ()) Arr=list (map (int, input (). Split ())) RET=0 mergesort (arr)Print("YES" ifRet% 2 = = 0Else "NO")
Hackerrank "Larry's Array"