{ }codelper
slicing listsremainderP0032

Rotate a list right

The first line gives the count n and the number of steps k, and the second line gives n integers.
Rotate the list right by k steps and print it on one line, separated by single spaces.
One step moves the last value to the front (1 2 3 4 5 rotated by 2 is 4 5 1 2 3).
k can be larger than n, and rotating by n gives the original list.

Input
1 ≤ n ≤ 100, 0 ≤ k ≤ 1,000,000,000, -1000 ≤ each integer ≤ 1000
Output
the rotated list on one line
Example 1 · stdin
5 2
1 2 3 4 5
stdout
4 5 1 2 3
Example 2 · stdin
5 7
1 2 3 4 5
stdout
4 5 1 2 3
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

Reduce first with k %= n.
Rotating one step k times is too slow for k = 10^9, and without reducing, Python a[-k:] + a[:-k] makes a[-k:] the whole list when k > n, so nothing moves.
Slicing: Python a[n-k:] + a[:n-k], JS a.slice(n - k), C# a[(n-k)..], C++ std::rotate, Craykon a.GetRange(n - k, k).
In the index formula (i - k + n) % n, dropping + n breaks C, C++, Java, C# and JS: -1 % 5 is -1 there, so you read outside the array (only Python gives 4).

Pythonmain.py · 4 lines
n, k = map(int, input().split())a = input().split()k %= nprint(" ".join(a[n - k:] + a[:n - k]))
JavaScriptmain.js · 5 lines
const [first, second] = require("fs").readFileSync(0, "utf8").split("\n");const [n, k0] = first.trim().split(/\s+/).map(Number);const a = second.trim().split(/\s+/);const k = k0 % n;console.log([...a.slice(n - k), ...a.slice(0, n - k)].join(" "));
TypeScriptmain.ts · 6 lines
import { readFileSync } from "node:fs";const [first, second] = readFileSync(0, "utf8").split("\n");const [n, k0]: number[] = first.trim().split(/\s+/).map(Number);const a: string[] = second.trim().split(/\s+/);const k: number = k0 % n;console.log([...a.slice(n - k), ...a.slice(0, n - k)].join(" "));
JavaMain.java · 15 lines
import java.util.Scanner;import java.util.StringJoiner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        int k = sc.nextInt() % n;        int[] a = new int[n];        for (int i = 0; i < n; i++) a[i] = sc.nextInt();        StringJoiner out = new StringJoiner(" ");        for (int i = 0; i < n; i++) out.add(String.valueOf(a[(i - k + n) % n]));        System.out.println(out);    }}
C#Main.cs · 13 lines
using System;using System.Linq; class Program{    static void Main()    {        int[] nk = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();        int n = nk[0], k = nk[1] % n;        string[] a = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        Console.WriteLine(string.Join(" ", a[(n - k)..].Concat(a[..(n - k)])));    }}
C++main.cpp · 13 lines
#include <algorithm>#include <iostream>#include <vector> int main() {    int n, k;    std::cin >> n >> k;    std::vector<int> a(n);    for (int& x : a) std::cin >> x;    std::rotate(a.begin(), a.end() - k % n, a.end());    for (int i = 0; i < n; i++) std::cout << (i ? " " : "") << a[i];    std::cout << "\n";}
Cmain.c · 14 lines
#include <stdio.h> int main(void) {    int n, k, a[100];    scanf("%d %d", &n, &k);    for (int i = 0; i < n; i++) scanf("%d", &a[i]);    k %= n;    for (int i = 0; i < n; i++) {        if (i > 0) putchar(' ');        printf("%d", a[(i - k + n) % n]);    }    putchar('\n');    return 0;}
Craykonmain.cray · 10 lines
nk = Console.ReadNumbers();n = nk[0];k = nk[1] % n;a = Console.ReadNumbers();result = a.GetRange(n - k, k);foreach (x in a.GetRange(0, n - k)){    result.Add(x);}Console.WriteLine(result);