C#
array rotation
programming
coding
algorithms

Rotating right an array of int in c?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Right-rotating an array by k positions means moving the last k elements to the front. For example, rotating [1, 2, 3, 4, 5] right by 2 gives [4, 5, 1, 2, 3]. In C#, you can achieve this with a temporary array, the reversal algorithm (in-place), LINQ, or Array.Copy. The reversal algorithm is the most efficient for in-place rotation with O(n) time and O(1) extra space.

Approach 1: Temporary Array

Create a new array and place elements at their rotated positions:

csharp
1static int[] RotateRight(int[] arr, int k)
2{
3    int n = arr.Length;
4    if (n == 0) return arr;
5
6    k = k % n;  // Handle k > n
7    if (k == 0) return (int[])arr.Clone();
8
9    int[] result = new int[n];
10
11    // Copy last k elements to the front
12    Array.Copy(arr, n - k, result, 0, k);
13    // Copy remaining elements after them
14    Array.Copy(arr, 0, result, k, n - k);
15
16    return result;
17}
18
19int[] arr = { 1, 2, 3, 4, 5 };
20int[] rotated = RotateRight(arr, 2);
21// rotated: [4, 5, 1, 2, 3]

Time: O(n). Space: O(n) for the new array.

Approach 2: Reversal Algorithm (In-Place)

Reverse three segments of the array to achieve the rotation without extra space:

csharp
1static void RotateRightInPlace(int[] arr, int k)
2{
3    int n = arr.Length;
4    if (n == 0) return;
5
6    k = k % n;
7    if (k == 0) return;
8
9    // Step 1: Reverse the entire array
10    Array.Reverse(arr, 0, n);
11    // [1,2,3,4,5] → [5,4,3,2,1]
12
13    // Step 2: Reverse the first k elements
14    Array.Reverse(arr, 0, k);
15    // [5,4,3,2,1] → [4,5,3,2,1]
16
17    // Step 3: Reverse the remaining n-k elements
18    Array.Reverse(arr, k, n - k);
19    // [4,5,3,2,1] → [4,5,1,2,3]
20}
21
22int[] arr = { 1, 2, 3, 4, 5 };
23RotateRightInPlace(arr, 2);
24// arr: [4, 5, 1, 2, 3]

Time: O(n). Space: O(1). This is the optimal approach when you need to rotate in place.

Approach 3: LINQ

csharp
1using System.Linq;
2
3static int[] RotateRightLinq(int[] arr, int k)
4{
5    int n = arr.Length;
6    if (n == 0) return arr;
7    k = k % n;
8
9    return arr.Skip(n - k).Concat(arr.Take(n - k)).ToArray();
10}
11
12int[] arr = { 1, 2, 3, 4, 5 };
13int[] rotated = RotateRightLinq(arr, 2);
14// [4, 5, 1, 2, 3]

Concise and readable but allocates a new array. Best for small arrays or when readability matters more than performance.

Approach 4: One-by-One Rotation

Rotate by 1 position, repeated k times:

csharp
1static void RotateRightByOne(int[] arr)
2{
3    int last = arr[arr.Length - 1];
4    Array.Copy(arr, 0, arr, 1, arr.Length - 1);
5    arr[0] = last;
6}
7
8static void RotateRightSlow(int[] arr, int k)
9{
10    k = k % arr.Length;
11    for (int i = 0; i < k; i++)
12        RotateRightByOne(arr);
13}

Time: O(n * k). This is the least efficient approach. Avoid for large arrays or large k values.

Approach 5: Span-Based (Modern C#)

Using Span<T> for zero-allocation rotation:

csharp
1static void RotateRightSpan(Span<int> span, int k)
2{
3    int n = span.Length;
4    if (n == 0) return;
5    k = k % n;
6    if (k == 0) return;
7
8    span.Reverse();
9    span[..k].Reverse();
10    span[k..].Reverse();
11}
12
13int[] arr = { 1, 2, 3, 4, 5 };
14RotateRightSpan(arr.AsSpan(), 2);
15// arr: [4, 5, 1, 2, 3]

Span<T> works with arrays, stack-allocated memory, and slices without copying.

Left Rotation vs Right Rotation

Right rotation by k is equivalent to left rotation by (n - k):

csharp
1// Right rotate by 2: [1,2,3,4,5] → [4,5,1,2,3]
2// Left rotate by 3:  [1,2,3,4,5] → [4,5,1,2,3]  (same result)
3
4static void RotateLeftInPlace(int[] arr, int k)
5{
6    int n = arr.Length;
7    k = k % n;
8    // Convert left rotation to right rotation
9    RotateRightInPlace(arr, n - k);
10}

Performance Comparison

ApproachTimeSpaceIn-PlaceReadability
Temporary arrayO(n)O(n)NoGood
Reversal algorithmO(n)O(1)YesMedium
LINQO(n)O(n)NoBest
One-by-oneO(n*k)O(1)YesSimple
Span-basedO(n)O(1)YesGood

Common Pitfalls

  • Forgetting k = k % n: If k is larger than the array length, the rotation wraps around. Without the modulo operation, Array.Copy throws ArgumentOutOfRangeException and the reversal algorithm reverses the wrong segments.
  • Empty array or k equals 0: Always check for arr.Length == 0 and k == 0 before performing rotation. Passing an empty array to Array.Reverse is fine but index calculations may produce invalid ranges.
  • Off-by-one in Array.Copy: Array.Copy(src, srcIndex, dst, dstIndex, length) copies length elements starting from srcIndex. Getting the indices wrong shifts elements to the wrong positions without throwing an error.
  • Confusing left and right rotation: Right rotation moves the last elements to the front. Left rotation moves the first elements to the end. Double-check which direction is requested before implementing.
  • Negative k values: k % n in C# preserves the sign for negative numbers (-2 % 5 == -2). Handle negative k by converting: k = ((k % n) + n) % n.

Summary

  • Use the reversal algorithm for O(n) time and O(1) space in-place rotation
  • Use Array.Copy with a temporary array for a simple, readable implementation
  • Use LINQ (Skip/Take/Concat) for one-liner readability in non-performance-critical code
  • Always apply k = k % n to handle rotation amounts larger than the array length
  • Right rotation by k equals left rotation by (n - k)
  • Handle edge cases: empty arrays, k equals 0, and negative k values

Course illustration
Course illustration

All Rights Reserved.