Hackerrank "Larry's Array"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.