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:
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:
Time: O(n). Space: O(1). This is the optimal approach when you need to rotate in place.
Approach 3: LINQ
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:
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:
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):
Performance Comparison
| Approach | Time | Space | In-Place | Readability |
| Temporary array | O(n) | O(n) | No | Good |
| Reversal algorithm | O(n) | O(1) | Yes | Medium |
| LINQ | O(n) | O(n) | No | Best |
| One-by-one | O(n*k) | O(1) | Yes | Simple |
| Span-based | O(n) | O(1) | Yes | Good |
Common Pitfalls
- Forgetting
k = k % n: Ifkis larger than the array length, the rotation wraps around. Without the modulo operation,Array.CopythrowsArgumentOutOfRangeExceptionand the reversal algorithm reverses the wrong segments. - Empty array or k equals 0: Always check for
arr.Length == 0andk == 0before performing rotation. Passing an empty array toArray.Reverseis fine but index calculations may produce invalid ranges. - Off-by-one in Array.Copy:
Array.Copy(src, srcIndex, dst, dstIndex, length)copieslengthelements starting fromsrcIndex. 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 % nin 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.Copywith 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 % nto 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

