Header Ad

HackerRank Left Rotation problem solution

In this HackerRank Left Rotation problem, we need to develop a program in which we have given an integer d and we need to rotate the array d times in left and return the result.

HackerRank Left Rotation problem solution

Problem solution in Python programming.

n,d = (int(x) for x in input().split())
l = [int(x) for x in input().split()]
length = len(l)
new = [0 for x in l]
for i in range(length):
    o = i-d
    new[o]=str(l[i])
print(" ".join(new))




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 scanner = null;
        int[] array = null;
        int rotations = 0;
        try {
            scanner = new Scanner(System.in);
            String firstLineStr = scanner.nextLine();
            String[] firstLineSplit = firstLineStr.split(" ");
            int arrayLength = Integer.parseInt(firstLineSplit[0]);
            array = new int[arrayLength];
            rotations = Integer.parseInt(firstLineSplit[1]);
            
            String secondLineStr = scanner.nextLine();
            String[] secondLineSplit = secondLineStr.split(" ");
            for(int i = 0; i < secondLineSplit.length; i++) {
                array[i] = Integer.parseInt(secondLineSplit[i]);
            }
        } finally {
            scanner.close();
        }
        
        for(int i = 0; i < array.length; i++) {
            int index = (i + rotations) % array.length;
            System.out.print(array[index] + " ");
        }
    }
}



Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int N, d; cin >> N >> d;
    vector<int> v(N);
    for (size_t i = 0; i < v.size(); ++i) {
        cin >> v[i];
    }
    d = d % N;
    for (int i = d; i < N; ++i)
        cout << v[i] << ' ';
    for (int i = 0; i < d; ++i)
        cout << v[i] << ' ';
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}



Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n; 
    scanf("%d",&n);
    int d;
    scanf("%d",&d);
    int *arr = malloc(sizeof(int) * n);
    
    for(int arr_i = 0; arr_i < n; arr_i++){
       scanf("%d",&arr[arr_i]);
    }
    
    int *arr2 = malloc(sizeof(int) * n);
    int curr=0;
    for(int i=d; i<n; i++) {
        arr2[curr] = arr[i];
        curr++;
    }
    for(int i=0; i<d; i++) {
        arr2[curr] = arr[i];
        curr++;
    }
    
    for(int i=0; i<n; i++) {
        printf("%d ",arr2[i]);
    }
    
    return 0;
}



Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    let lines = input.split('\n');
    let arr   = lines[1].split(' ').map(Number);
    let l     = lines[0].split(' ');
    let n     = l[0];
    let d     = l[1];

    let len = arr.length;
    if (d === len) {
        console.log(copy.join(' '));
    } else {
        if (d > len) d = d % len;
    
        let left   = arr.slice(0, d);
        let right  = arr.slice(d);
        let result = [...right, ...left].join(' ');

        console.log(result);
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

1 Comments