Larry has been given a permutation of a sequence of natural numbers incrementing from 1 as an array. He must determine whether the array can be sorted using the following operation any number of times:
- Choose any 3 consecutive indices and rotate their elements in such a way that .
For example, if :
A rotate
[1,6,5,2,4,3] [6,5,2]
[1,5,2,6,4,3] [5,2,6]
[1,2,6,5,4,3] [5,4,3]
[1,2,6,3,5,4] [6,3,5]
[1,2,3,5,6,4] [5,6,4]
[1,2,3,4,5,6]
YES
On a new line for each test case, print YES if A can be fully sorted. Otherwise, print NO
.
Example:
Input: n = 3, A = {3, 1, 2}
Output: YES
Approach
C++
#include <bits/stdc++.h>using namespace std;string larrysArray(vector<int> A){int a = 0;for (int j = 0; j < A.size(); j++){for (int k = j + 1; k < A.size(); k++){if (A[j] > A[k])a += 1;}}if (a % 2 == 0)return "YES";elsereturn "NO";}int main(){int n = 3;vector<int> A = {3, 1, 2};cout << larrysArray(A) << "\n";return 0;}
No comments:
Post a Comment