i not able logic behind solution problem . thankful if can explain me working of it.
solution:
#include <bits/stdc++.h> using namespace std; const int n=1509; int n; int a[n]; void input(){ scanf("%d",&n); (int i=1;i<=n;i++) scanf("%d",&a[i]); } void sol(){ int k=1; (int i=1;i<=n;i++) (int j=i+1;j<=n;j++) k^=(a[i]>a[j]); if (k) printf("yes\n"); else printf("no\n"); } int main() { int test; scanf("%d",&test); while (test--){ input(); sol(); } return 0; }
i not able how after xoring each permutation, value of 'k' in end determining answer(ie whether can arranged in sorting order) ?
when rotate block change number of inversions +/- 2 or 0 (work out on paper, if don't trust me). if number of inversions in array odd not able make sorted given operation. you'll end array sorted 2 elements in place (1 inversion) , can't fix given operation.
what code check if number of inversions odd xoring every time sees inversion. can same result if count inversions , check inversions % 2 == 0
.
Comments
Post a Comment