Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to rotate an array k time using C#?
Array rotation is a common programming problem where elements are shifted by k positions. When we rotate an array to the right by k positions, the last k elements move to the beginning, and the remaining elements shift right.
For example, if we have array [1, 2, 3, 4, 5] and rotate it right by 2 positions, we get [4, 5, 1, 2, 3].
Algorithm
The most efficient approach uses the reversal algorithm which works in three steps −
Reverse the entire array
Reverse the first k elements
Reverse the remaining elements from position k to end
Using Reversal Algorithm
Example
using System;
public class ArrayRotation {
public static void RotateArray(int[] arr, int k) {
int n = arr.Length;
k = k % n; // Handle cases where k > array length
if (k == 0) return; // No rotation needed
// Step 1: Reverse entire array
Reverse(arr, 0, n - 1);
// Step 2: Reverse first k elements
Reverse(arr, 0, k - 1);
// Step 3: Reverse remaining elements
Reverse(arr, k, n - 1);
}
private static void Reverse(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
public static void Main(string[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
Console.WriteLine("Original array: " + string.Join(", ", arr));
RotateArray(arr, 3);
Console.WriteLine("After rotating by 3: " + string.Join(", ", arr));
// Example with different array
int[] arr2 = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
Console.WriteLine("\nOriginal array: " + string.Join(", ", arr2));
RotateArray(arr2, 3);
Console.WriteLine("After rotating by 3: " + string.Join(", ", arr2));
}
}
The output of the above code is −
Original array: 1, 2, 3, 4, 5, 6, 7 After rotating by 3: 5, 6, 7, 1, 2, 3, 4 Original array: 9, 8, 7, 6, 5, 4, 3, 2, 1 After rotating by 3: 3, 2, 1, 9, 8, 7, 6, 5, 4
Using Built-in Methods
Example
using System;
using System.Linq;
public class ArrayRotationBuiltIn {
public static int[] RotateArrayBuiltIn(int[] arr, int k) {
int n = arr.Length;
k = k % n;
if (k == 0) return arr;
// Take last k elements and first n-k elements
return arr.Skip(n - k).Concat(arr.Take(n - k)).ToArray();
}
public static void Main(string[] args) {
int[] original = { 1, 2, 3, 4, 5, 6, 7 };
int[] rotated = RotateArrayBuiltIn(original, 3);
Console.WriteLine("Original: " + string.Join(", ", original));
Console.WriteLine("Rotated by 3: " + string.Join(", ", rotated));
// Test with edge case
int[] test = { 10, 20, 30 };
int[] result = RotateArrayBuiltIn(test, 5); // k > array length
Console.WriteLine("\nOriginal: " + string.Join(", ", test));
Console.WriteLine("Rotated by 5: " + string.Join(", ", result));
}
}
The output of the above code is −
Original: 1, 2, 3, 4, 5, 6, 7 Rotated by 3: 5, 6, 7, 1, 2, 3, 4 Original: 10, 20, 30 Rotated by 5: 20, 30, 10
Comparison
| Approach | Time Complexity | Space Complexity | In-place |
|---|---|---|---|
| Reversal Algorithm | O(n) | O(1) | Yes |
| Built-in Methods | O(n) | O(n) | No |
Conclusion
Array rotation can be efficiently achieved using the reversal algorithm with O(1) space complexity, or using built-in LINQ methods for cleaner code. The reversal approach is preferred for memory-efficient solutions, while LINQ methods offer better readability.
