Array Left Rotation Hackerrank problem solution

Array Left Rotation Hackerrank

A left rotation operation on an array of size n shifts each of the array’s elements 1 unit to the left. For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2].

Read Full Question at:

https://www.hackerrank.com/challenges/array-left-rotation/forum

Given an array of n integers and a number, d, perform the left rotations on the array. Then print the updated array as a single line of space-separated integers.

Input Format

The first line contains two space-separated integers denoting the respective values of n (the number of integers) and d(the number of left rotations you must perform).
The second line contains n space-separated integers describing the respective elements of the array’s initial state.

Constraints

1<=n<=105
1<=d<=n
1<=ai<=106
Output Format

Print a single line of n space-separated integers denoting the final state of the array after performing d left rotations.

Sample Input

5 4
1 2 3 4 5

Sample Output

5 1 2 3 4

Array Left Rotation Hackerrank

Solution

Now you understand what is the problem, we just have to shift the elements of an array to the left as many times as our second input value. so if the array is [7 6 8 9 ]  and shift-value d=2 this means for the first time array will be [ 6 8 9 7] and after second rotation it will be [ 8 9 7 6].

Let’s think about how we will do it in programming. First logic is very easy: just take input in the array and take the value of d. if d>n  we will take the remainder because after n rotation array will be the same. so d=d%n  now we are following the position print out concept.

what is position print out concept?

because we know the value of d so, we will print that element whose index is d.

again see the above example.  a=[7 6 8 9 ]  and d=2

here see carefully a[d]=a[2]=8 so we will print 8 then 9. i.e; first loop will be from d to the last index and the second loop will be from beginning to d.

8 and 9 from the first loop, and 7 6 from the second loop.

 C++ code

    int n,d;
    cin>>n;
    int a[n];
    cin>>d;
    for(int i=0;i<n;cin>>a[i],i++);
    d=d%n;
    for(int i=d;i<n;i++)
        cout<<a[i]<<" ";
    
    for(int i=0;i<d;i++)
         cout<<a[i]<<" ";
    return 0;

Java and Python will be the same

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int d = scan.nextInt();
        int[] a= new int[n];
    for(int i=0;i<n;a[i]=scan.nextInt(),i++);
   	 d=d%n;
    for(int i=d;i<n;i++)
        System.out.print(a[i]+" ");
    
    for(int i=0;i<d;i++)
         System.out.print(a[i]+" ");
    }
    
  }

IInd Logic:

This is the advance logic, and it’s based on mathematics.If we want 1 left rotation we will make n-1 right rotations. and d left rotations will be n-d right rotations.

0<i<n-1   here i is index and n is the size of the array. so we will replace or store every ith  to (i+n-d)th   place. then we will display it for the output. here (i+n-d)%n for d>n you have read above.

Array Left Rotation Hackerrank

whenever I read an element of the array from the input, I directly assign it int to the array at the specified index after shifting.   The array of size n, so when the index (i+n-d) is equal to n, the index actually goes back to the starting index. I use mod(%) to get the correct index d>n;

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int n,d,temp;
    cin>>n>>d;
    int *a =new int[n];
   for(int i=0; i<n;i++)
       cin>>a[(i+n-d)%n];
   
    for(int i=0; i<n;i++)
        cout<<a[i]<<" ";
     
    return 0;
}
int n,d;
    cin>>n;
    int a[n];
    cin>>d;
    for(int i=0;i<n;cin>>a[i],i++);
    d=d%n;
    for(int i=d;i<n;i++)
        cout<<a[i]<<" ";
    
    for(int i=0;i<d;i++)
         cout<<a[i]<<" ";
    return 0;

Keep in mind a left rotation of x is equal to (len-x) of right rotations. Hence, it would be more efficient to the rotation with least amount of “moves”.

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int d = scan.nextInt();
        int[] array = new int[n];
        for(int i=0; i<n;i++) {
            array[(i+n-d)%n] = scan.nextInt();
        }
        for(int i=0; i<n;i++) {
            System.out.print(array[i] + " ");
        }      
    }
}
public static void main(String[] args) {       
        Scanner sc = new Scanner(System.in);        
        int n = sc.nextInt();
        int d = sc.nextInt();
        int array[] = new int[n];        
        for (int i=0; i<n; i++) {  array[i] = sc.nextInt();  }
        for (int i=0; i<n; i++) {  System.out.print(array[(i+d)%n] + " "); }
    }
n, d = map(int, raw_input().split())

arr = map(int, raw_input().split())

arr_dup = arr[:]

for i in range(n):
	arr[i] = arr_dup[(i+d)%n]

for i in arr:
	print i,
# Enter your code here. Read input from STDIN. Print output to STDOUT
n,m=raw_input().split()
a=[int(i) for i in raw_input().split()]
for j in range(int(n)):
    print a[(j+int(m))%int(n)],

Video tutorial for this article is also available

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.