GithubHelp home page GithubHelp logo

cycle-swap's Introduction

Cycle Swap

The purpose of this exercise is to train you to work with arrays.

Estimated workload of this exercise is 30 min.

Description

Please, proceed to CycleSwap class and implement its static methods:

  • void cycleSwap(int[] array)
    Shifts all the elements in the given array to the right by 1 position.
    In this case, the last array element becomes first.
    For example, 1 3 2 7 4 becomes 4 1 3 2 7.

  • void cycleSwap(int[] array, int shift)
    Shift all the elements in the given array to the right in the cycle manner by shift positions.
    Shift value is guaranteed to be non-negative and not bigger than the array length.
    For example, 1 3 2 7 4 with a shift of 3 becomes 2 7 4 1 3.

For a greater challenge, try not using loops in your code (not mandatory).

Note that input array may be empty.

Examples


Code Sample:

...
int[] array = new int[]{1, 3, 2, 7, 4};
CycleSwap.cycleSwap(array);
System.out.println(Arrays.toString(array));

Output:

[4, 1, 3, 2, 7]

Code Sample:

...
int[] array = new int[]{1, 3, 2, 7, 4};
CycleSwap.cycleSwap(array, 2);
System.out.println(Arrays.toString(array));

Output:

[7, 4, 1, 3, 2]

Code Sample:

...
int[] array = new int[]{1, 3, 2, 7, 4};
CycleSwap.cycleSwap(array, 5);
System.out.println(Arrays.toString(array));

Output:

[1, 3, 2, 7, 4]

cycle-swap's People

Contributors

thejerome avatar patocskaierik avatar

Watchers

 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.