Легко Решено: 0 Процент решений: 0%

Rotate Array

Rotate a list to the right by some steps.

Чему вы научитесь

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

Условие

Показать перевод

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.

Ограничения

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

Примеры

Пример 1
Ввод:
numbers = [1, 2, 3, 4, 5]
steps = 2

Ожидаемый вывод:
expected = [4, 5, 1, 2, 3]
Ввод:
std::vector<long long> numbers = {1, 2, 3, 4, 5};
long long steps = 2;

Ожидаемый вывод:
std::vector<long long> expected = {4, 5, 1, 2, 3};
Ввод:
long[] numbers = new long[] {1, 2, 3, 4, 5};
long steps = 2;

Ожидаемый вывод:
long[] expected = new long[] {4, 5, 1, 2, 3};

Пояснение

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

Пример 2
Ввод:
numbers = [1, 2, 3]
steps = 3

Ожидаемый вывод:
expected = [1, 2, 3]
Ввод:
std::vector<long long> numbers = {1, 2, 3};
long long steps = 3;

Ожидаемый вывод:
std::vector<long long> expected = {1, 2, 3};
Ввод:
long[] numbers = new long[] {1, 2, 3};
long steps = 3;

Ожидаемый вывод:
long[] expected = new long[] {1, 2, 3};

Пояснение

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

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

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

Подсказка 2

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

Подсказка 3

Handle very large step counts by reducing them before rearranging.

Ожидаемая сложность

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

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

Принятых решений пока нет. Решите задачу и займите первое место.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.