Neosify - Buy, Stake & Earn Crypto
Neosify - Buy, Stake & Earn Crypto
Neosify - Buy, Stake & Earn Crypto
Dcoder logo

Rotate Array @ Dcoder

By Lochard | As a newbie programmer | 2 Mar 2023


Problem: You are given an array of N length. You have to rotate the array rightwards by K rotations, that is, shift each element to the right by K positions. Print the rotated array.

Input: First line contains N and K.

Second line contains N integers denoting the array.

Output: Print the array after the rotation.

Constraints: 1 <= N, K <= 100000

1 <= Arr[i] <= 10^9

Sample Input: 5 2

1 2 3 4 5

 

Sample Output: 

4 5 1 2 3

from collections import deque

k = int(input().split()[1])
a = deque(input().split())
a.rotate(k)
print(' '.join(a))

How do you rate this article?

7


Lochard
Lochard

Asylum seeker in Lithuania. My short-lifed daily on GitHub https://github.com/locharp/asylum_daily/tree/main/en


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.