In this HackerRank Larry’s Array problem, Larry has been given a permutation of a sequence of natural numbers incrementing from 1 as an array. On a new line for each test case, print YES if A can be fully sorted. Otherwise, print NO.
Problem solution in Python programming.
#!/bin/python3 import sys def canSort(l): s = sorted(l) for i in s[:-2]: if l.index(i) % 2: l.remove(i) l[0],l[1]=l[1],l[0] else: l.remove(i) if l[0]<l[1]: return True else: return False if __name__ == "__main__": t = int(sys.stdin.readline().strip()) for _ in range(t): n = int(sys.stdin.readline().strip()) A = list(map(int, sys.stdin.readline().split())) if canSort(A): print("YES") else: print("NO")
Problem solution in Java Programming.
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner scan = new Scanner(System.in); int c = Integer.parseInt(scan.nextLine()); for(int i=0;i<c;i++){ int n = Integer.parseInt(scan.nextLine()); String[] sp = scan.nextLine().split("\s+"); LinkedList<Integer> ll = new LinkedList<Integer>(); for(int j=0;j<n;j++)ll.add(Integer.parseInt(sp[j])); for(int j=0;j<n-2;j++){ if(ll.get(j)!=j+1){ int index = findIndex(ll,j+1); if((index-j)%2==0){ int val = ll.remove(index); ll.add(j,val); } else{ int val =ll.remove(index); ll.add(j,val); int tmp = ll.remove(j+2); ll.add(j+1,tmp); } } } String res = "YES"; if(ll.get(n-1) != n)res = "NO"; System.out.println(res); } } public static int findIndex(LinkedList<Integer> ll, int val){ for(int i=val;i<ll.size();i++)if(ll.get(i)==val)return i; return -1; } }
Problem solution in C++ programming.
#include <vector> #include <algorithm> #include <cmath> #include <string> #include <sstream> #include <iostream> #include <iomanip> #include <queue> #include <map> #include <set> #include <list> #include <utility> #include <numeric> #include <fstream> using namespace std; #define ALL(c) (c).begin(),(c).end() #define SZ(c) int((c).size()) #define LEN(s) int((s).length()) #define FOR(i,n) for(int i=0;i<(n);++i) #define FORD(i,a,b) for(int i=(a);i<=(b);++i) #define FORR(i,a,b) for(int i=(b);i>=(a);--i) typedef istringstream iss; typedef ostringstream oss; typedef long double ld; typedef long long i64; typedef pair<int,int> pii; typedef vector<i64> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<vvvi> vvvvi; typedef vector<string> vs; int main() { //freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(0); int T; cin >> T; FOR(tt, T) { int n; cin >> n; vi a(n); FOR(i, n) cin >> a[i]; int r = 0; FOR(i, n) FORD(j, i+1, n-1) if (a[i] > a[j]) r++; cout << (r % 2 == 0 ? "YES" : "NO") << endl; } return 0; }
Problem solution in C programming.
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int t; scanf("%d", &t); for (int a0 = 0; a0 < t; a0++){ int n; scanf("%d", &n); int A[n]; for (int i = 0; i < n; i++){ scanf("%d", &A[i]); } int aux, permutation = 0; for (int i = 0; i < n - 1; i++){ if (A[i] != i + 1){ for (int j = i + 1; j < n; j++){ if (A[j] == i + 1){ aux = A[i]; A[i] = A[j]; A[j] = aux; permutation++; } } } } if (permutation % 2 == 0){ printf("YES"); } else { printf("NO"); } printf("n"); } return 0; }
Problem solution in JavaScript programming.
function processData(input) { var lines = input.split('n'); for (var c=0; c<parseInt(lines[0]); c++) { var len = parseInt(lines[c*2+1]); var arr = lines[c*2+2].split(' ').map(function(n){return parseInt(n)}); var inv = 0; for (var i=0; i<arr.length; i++) { for (var j=i; j<arr.length; j++) { if (arr[i]>arr[j]) { inv++ } } } console.log((inv%2===0)?'YES':'NO'); } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });