Rotate List Right

Rotate List Right

Medium Python Lists 18 views
Explanation Complexity

Problem Statement

Read n integers and k. Rotate list to the right by k positions and output.

Input Format

Line1 n k. Line2 n integers.

Output Format

Rotated list.

Example

5 2
1 2 3 4 5
4 5 1 2 3

Constraints

1

Input / Output Format

Input Format
Line1 n k. Line2 n integers.
Output Format
Rotated list.
Constraints
1

Examples

Input:
5 2 1 2 3 4 5
Output:
4 5 1 2 3

Example Solution (Public)

Python
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))

Official Solution Code

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))
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.