0 votes
1 view
in Python by (45.1k points)

For example, if N=2, given the input array [1, 2, 3, 4, 5, 6]

the function should return [5, 6, 1, 2, 3, 4] 

if N=3 then [4, 5, 6, 1, 2, 3]

1 Answer

0 votes
by (16.8k points)

Check this code:

def rotate(array, n):

    length = len(array)

    if n > 0:

        tail = array[length - n:]

        head = array[0:length - n]

        new_array = tail + head

    else:

        tail = array[- n:]

        head = array[0:- n]

        new_array = tail + head

    print(new_array)

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...