intermediate · arrays · in place · reversal

Shift the schedule by k days

Problem

A circular schedule is stored in an array. Rotate it k positions to the right in place: the last k entries move to the front, keeping their order.

Constraints

  • 1 ≤ n ≤ 32
  • 0 ≤ k ≤ 100 (k may exceed n)
  • O(1) extra space

Examples

[1, 2, 3, 4, 5, 6, 7], k = 3 → [5, 6, 7, 1, 2, 3, 4]

Show the solution

Three reversals

Reversing everything puts the last k entries in front, but backwards; reversing each part separately fixes the order inside both parts.

Why it is correct

Write the array as A·B with |B| = k. reverse(A·B) = rev(B)·rev(A); reversing each part gives B·A, the rotation.

Complexity

Time O(n), extra space O(1). Each element is swapped at most twice.

For contrast: Copy into a new array at (i + k) mod n, or rotate one step k times. That costs O(n), O(n · k) time and O(n), O(1) space.

Edge cases

  • k = 0 or a multiple of n: unchanged.
  • k > n: use k mod n.
  • One element: unchanged.

Common mistakes

  • Forgetting k mod n (out-of-range indices).
  • Rotating left instead of right.

Step through it

The solution in four languages

Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.

namespace Bekadan.Dsa.Examples.Problems;
 
public static class RotateArray
{
    /// <summary>Rotates right by k in place with three reversals. Precondition: values is not empty. O(n), O(1) space.</summary>
    public static void Rotate(int[] values, int k)
    {
        int n = values.Length;
        k %= n; // rotating by n changes nothing
        Reverse(values, 0, n - 1);
        Reverse(values, 0, k - 1);
        Reverse(values, k, n - 1);
    }
 
    private static void Reverse(int[] values, int i, int j)
    {
        for (; i < j; i++, j--)
            (values[i], values[j]) = (values[j], values[i]);
    }
}

Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/RotateArray.cs

def rotate(values: list[int], k: int) -> None:
    """Rotate right by k in place with three reversals. Precondition: values is not empty. O(n), O(1) space."""
    n = len(values)
    k %= n  # rotating by n changes nothing
    _reverse(values, 0, n - 1)
    _reverse(values, 0, k - 1)
    _reverse(values, k, n - 1)
 
 
def _reverse(values: list[int], i: int, j: int) -> None:
    while i < j:
        values[i], values[j] = values[j], values[i]
        i, j = i + 1, j - 1

Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/rotate_array.py

package dev.bekadan.dsa.problems;
 
public final class RotateArray {
    private RotateArray() {
    }
 
    /** Rotates right by k in place with three reversals. Precondition: values is not empty. O(n), O(1) space. */
    public static void rotate(int[] values, int k) {
        int n = values.length;
        k %= n; // rotating by n changes nothing
        reverse(values, 0, n - 1);
        reverse(values, 0, k - 1);
        reverse(values, k, n - 1);
    }
 
    private static void reverse(int[] values, int i, int j) {
        for (; i < j; i++, j--) {
            int tmp = values[i];
            values[i] = values[j];
            values[j] = tmp;
        }
    }
}

Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/RotateArray.java

#pragma once
 
#include <cstddef>
#include <span>
#include <utility>
 
namespace bekadan::dsa::problems {
 
inline void reverse_range(std::span<int> values, std::ptrdiff_t i, std::ptrdiff_t j) {
    for (; i < j; ++i, --j)
        std::swap(values[static_cast<std::size_t>(i)], values[static_cast<std::size_t>(j)]);
}
 
// Rotates right by k in place with three reversals (std::rotate does the same job). Precondition: not empty. O(n).
inline void rotate_array(std::span<int> values, std::ptrdiff_t k) {
    const auto n = std::ssize(values);
    k %= n;  // rotating by n changes nothing
    reverse_range(values, 0, n - 1);
    reverse_range(values, 0, k - 1);
    reverse_range(values, k, n - 1);
}
 
}  // namespace bekadan::dsa::problems

Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/rotate_array.hpp