Rotate List Right
Programming Interview
Medium
5 views
Problem Description
Input provides {x}. Rotate list to the right by k positions and output.
Input Format
Line1 n k. Line2 n integers.
Output Format
Rotated list.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<2: sys.exit(0)
n,k=map(int,lines[0].split())
a=lines[1].split()[:n]
if n==0:
sys.stdout.write('')
else:
k%=n
res=a[-k:]+a[:-k] if k else a
sys.stdout.write(' '.join(res))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!