Array Challenge | Sololearn: Learn to code for FREE!

+9

Array Challenge

A left rotation operation on an array of size shifts each of the array's elements unit to the left. Given an array of integers and a number, perform left rotations on the array. Then print the updated array as a single line of space-separated integers. 1) Input Format The first line contains two space-separated integers denoting the respective values of (the number of integers) and (the number of left rotations you must perform). The second line contains space-separated integers describing the respective elements of the array's initial state. 2) Output Format Print a single line of space-separated integers denoting the final state of the array after performing left rotations. 3) Sample Input 5 4 1 2 3 4 5 4) Sample Output 5 1 2 3 4 Explanation When we perform 4 left rotations, the array undergoes the following sequence of changes: [1,2,3,4,5] -> [2,3,4,5,1] -> [3,4,5,1,2] -> [4,5,1,2,3] -> [5,1,2,3,4] Thus, we print the array's final state as a single line of space-separated values, which is 5 1 2 3 4.

8/3/2017 12:56:47 PM

Akachi Ugwa

31 Answers

New Answer

+7

My answer https://code.sololearn.com/c2cilu9e0Lhd/#rb @Hatsy - requiring of input works for my code

+17

I was quite surprised that only one guy here went for the optimized solution. Hats off to @sayan chandra. give it an array of a million of elements and zillions of iterations it will still finish in one pass šŸ˜ƒšŸ˜ƒšŸ˜ƒ

+14

My try: https://code.sololearn.com/W02963eVHS07/?ref=app

+9

tl;dr - If Ruby can take an entire input and split it into array elements, why can't I? *Revised submission* https://code.sololearn.com/c0B5ssuIStZv/?ref=app

+8

The input for array elements wouldn't work well with loops on Code Playground. Requesting for change in kata specifications on input format. Let array contents be array index + 1 by default. Just let the user specify array size and rotation count.

+8

@Akachi Yeah. It is possible in Ruby because users can directly input an entire array. In C++, the array elements have to be inserted manually with a loop: for (int i = 0; i < array_size; i++) { cin >> array[i]; } ... unless... I may try to code another version. ;>

+7

Array size and rotation count is fine. Just the part about users inserting array elements may prove to be problematic with Code Playground. https://code.sololearn.com/cCiMU8vvENxh/?ref=app

+6

https://code.sololearn.com/cDIZ5znnWppq/?ref=app

+5

here is mine... i didnt rotate šŸ˜…šŸ˜…... just used a logic instead... here it is. GIVE INPUT AS 5 4 34 56 77 23 40 (example) https://code.sololearn.com/cE9W6Ggu3upA/?ref=app

+4

Just intended to demo leveraging Python's syntax for this task (there's a winner already so here as meta-FYI). Code lines 1-4 are explained in the tail comment. https://code.sololearn.com/clf9WfPNSB48/?ref=app

+3

My entry. Wanted to use STL rotate method https://code.sololearn.com/c7WLYgpRmHxV/?ref=app

+3

a little late https://code.sololearn.com/cSOBj9tdcm1z/?ref=app

+3

A solution to the Array Challenge written in JavaScript and presented with the aid of HTML and CSS. https://code.sololearn.com/W16SQ32xA5Sw/#html

+2

My first version https://code.sololearn.com/c6LKr84C5aeC/?ref=app

+2

@Akachi, don't mean to advertise, but for the sake of those who are always late for these (e.g. me), please post your coding challenge here https://www.sololearn.com/Discuss/583904/?ref=app

+2

Here is a c# solution using Copy and sayan's logic https://code.sololearn.com/czmUYh9e1ZFG/?ref=app

+2

+2

I don't understand, why do I get downvotes on my answer? You think it's better to iterate?

+2

What would be the advantage of << operator, I would have to do different shifts depending on type of array and still carry the values for left side, right?

+2

@Nikolay Sayan Chandra's approach was the one I also felt was appropriate for this HackerRank challenge (rotate a matrix by rings)...I thought I was the only one who did it, but the expert solution takes the approach that all you really need is a modulus-collapsed offset print (the rings are just a silly distraction--flatten them and it's the same problem): https://www.hackerrank.com/companies/hackerrank/challenges/matrix-rotation/problem