Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Larry’s Array problem solution

YASH PAL, 31 July 202414 December 2025

In this HackerRank Larry’s Array problem solution, 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 
    ABC -> BCA -> CAB -> ABC.

For example, if A = {1,6,5,2,4,3}:

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.

HackerRank Larry's Array problem solution

HackerRank Larry’s Array 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")

Larry’s Array 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);
});

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes