-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLarry's Array
36 lines (32 loc) · 975 Bytes
/
Larry's Array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//https://www.hackerrank.com/challenges/larrys-array/problem
/*It is based upon the inversions.(An inversion is when a tile precedes another tile with a lower number on it).
This questions is solvable only if no of inversions is even.
it is similar to 15 tiles problem: https://www.cs.bham.ac.uk/~mdr/teaching/modules04/java2/TilesSolvability.html*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,n;
cin >> t;
int count,i,j;
for(int a0 = 0; a0 < t; a0++)
{
count=0;
cin >> n;
int a[n];
//input
for(i=0; i < n; i++)
cin >> a[i];
//store the no of inversions in count
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
count++;
//solvable only if no of inversions is even
if(count%2==0)
cout << "YES" << endl;
else
cout<< "NO" <<endl;
}
return 0;
}