0w1

HR Larry's Array ( Math Invariant )

https://www.hackerrank.com/challenges/larrys-array
Rotating on any permutation, the number of inversions will have difference by exactly 2. Initially, the correct permutation has no inversion, which means only if the number of inversion is even, it can be rotated back to the correct permutation. That is the invariant.

#include <bits/stdc++.h>
using namespace std;

typedef vector< int > vi;
typedef vector< vi > vvi;

void solve(){
    int T; cin >> T;
    while( T-- ){
        int N; cin >> N;
        vi A( N );
        for( int i = 0; i < N; ++i )
            cin >> A[ i ];
        int inv_cnt = 0;
        for( int i = 0; i < N; ++i )
            for( int j = i + 1; j < N; ++j )
                inv_cnt += ( A[ i ] > A[ j ] );
        if( inv_cnt & 1 ) cout << "NO\n";
        else cout << "YES\n";
    }
}

int main(){
    ios::sync_with_stdio( false );
    cin.tie( 0 );
    cout.tie( 0 );

    solve();

    return 0;
}