Fácil 0 resueltos 0% de aceptación

Rotate Array

Rotate a list to the right by some steps.

Lo que aprenderás

  • Shift elements with index arithmetic
  • Reduce large rotations with modulo
  • Build a rotated copy of a list

Problema

Ver traducción

Rotate the list to the right by steps positions: the last element moves to the front, steps times.

Implement rotate_right(numbers, steps).

Input

numbers: The list of integers. steps: How many positions to rotate.

Output

Return the rotated list.

Restricciones

0 ≤ length ≤ 100,000; 0 ≤ steps ≤ 10⁹

Ejemplos

Ejemplo 1
Entrada:
numbers = [1, 2, 3, 4, 5]
steps = 2

Salida esperada:
expected = [4, 5, 1, 2, 3]
Entrada:
std::vector<long long> numbers = {1, 2, 3, 4, 5};
long long steps = 2;

Salida esperada:
std::vector<long long> expected = {4, 5, 1, 2, 3};
Entrada:
long[] numbers = new long[] {1, 2, 3, 4, 5};
long steps = 2;

Salida esperada:
long[] expected = new long[] {4, 5, 1, 2, 3};

Explicación

Two right rotations: [5, 1, 2, 3, 4], then [4, 5, 1, 2, 3].

Ejemplo 2
Entrada:
numbers = [1, 2, 3]
steps = 3

Salida esperada:
expected = [1, 2, 3]
Entrada:
std::vector<long long> numbers = {1, 2, 3};
long long steps = 3;

Salida esperada:
std::vector<long long> expected = {1, 2, 3};
Entrada:
long[] numbers = new long[] {1, 2, 3};
long steps = 3;

Salida esperada:
long[] expected = new long[] {1, 2, 3};

Explicación

Rotating by the length brings the list back: [1, 2, 3].

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

A rotation by the list length changes nothing, so only the remainder matters.

Pista 2

Think of splitting the list near the end and moving that tail to the front.

Pista 3

Handle very large step counts by reducing them before rearranging.

Complejidad esperada

O(n) time, O(n) extra space

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Clasificación

Clasificación completa

La mejor solución aprobada de cada programador. Las más rápidas, ligeras y mejor valoradas van primero.

Aún no hay soluciones aprobadas. Resuélvelo y quédate el primer puesto.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.