GithubHelp home page GithubHelp logo

reinelt88 / arrays-left-rotation Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 3 KB

Code challenge: Given an array α of η integers and a number, d, perform d left rotations

JavaScript 100.00%
codechallenge array array-manipulations javascript algorithms

arrays-left-rotation's Introduction

Arrays left rotation

Code challenge: Given an array α of η integers and a number, d, perform d left rotations A left rotation operation on an array 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]. Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.

Given an array α of η integers and a number, d, perform d left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.

Function Description

Complete the function rotLeft in the editor below.

rotLeft has the following parameter(s):

  • int a[n]: the array to rotate
  • int d: the number of rotations

Returns

  • int a'[n]: the rotated array

Input Format

The first line contains two space-separated integers η and d , the size of α and the number of left rotations.
The second line contains η space-separated integers, each an α[i].

Constraints

  • 1  ≤ η ≤ 10^5
  • 1  ≤ d ≤ η
  • 1  ≤ α[i] ≤ 10^6

Sample Input

5 4
1 2 3 4 5

Sample Output

5 1 2 3 4

Explanation

When we perform d = 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]

arrays-left-rotation's People

Contributors

reinelt88 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.