How to Rotate an Array to a Given Pivot in C#

aamasum
3 min readNov 24, 2024

--

Rotating an array is a common problem in programming where elements are shifted either left or right to align a specific pivot index. Here’s how you can do it in C#.

Problem Statement

Given an array and a pivot index, rearrange the array so that the pivot index becomes the first element, with the elements before the pivot moving to the end of the array.

For example:

int[] array = { 1, 2, 3, 4, 5, 6 };

int pivot = 2;

Output:

{ 3, 4, 5, 6, 1, 2 }

Approach 1: Using Array Slicing

In this approach, split the array into two parts based on the pivot and concatenate them.

using System;
class Program
{
static void Main()
{
int[] array = { 1, 2, 3, 4, 5, 6 };
int pivot = 2;
int[] rotatedArray = RotateArray(array, pivot);
Console.WriteLine(string.Join(", ", rotatedArray));
}
static int[] RotateArray(int[] array, int pivot)
{
if (pivot < 0 || pivot >= array.Length)
{
throw new ArgumentOutOfRangeException(nameof(pivot), "Pivot must be within the array bounds.");
}
int[] leftPart = array[pivot..]; // Elements from pivot to end
int[] rightPart = array[..pivot]; // Elements from start to pivot
return leftPart.Concat(rightPart).ToArray(); // Combine the two parts
}

}

Explanation:

  • The .. operator slices the array into two parts.
  • The Concat() method merges the two arrays.
  • The resulting array starts from the pivot index.

Approach 2: Using a Temporary Array

This approach creates a temporary array to rearrange the elements.

static int[] RotateArrayTemp(int[] array, int pivot)
{
int n = array.Length;
if (pivot < 0 || pivot >= n)
{
throw new ArgumentOutOfRangeException(nameof(pivot), "Pivot must be within the array bounds.");
}
int[] rotatedArray = new int[n];
int index = 0;
// Add elements from pivot to end
for (int i = pivot; i < n; i++)
{
rotatedArray[index++] = array[i];
}
// Add elements from start to pivot
for (int i = 0; i < pivot; i++)
{
rotatedArray[index++] = array[i];
}
return rotatedArray;
}

Advantages:

  • Provides clear control over the rotation process.
  • Avoids LINQ for developers working in restricted environments.

Approach 3: In-Place Rotation

This approach modifies the array in-place without additional memory allocation.

using System;
class Program
{
static void Main()
{
int[] array = { 1, 2, 3, 4, 5, 6 };
int pivot = 2;
RotateArrayInPlace(array, pivot);
Console.WriteLine(string.Join(", ", array));
}
static void RotateArrayInPlace(int[] array, int pivot)
{
if (pivot < 0 || pivot >= array.Length)
{
throw new ArgumentOutOfRangeException(nameof(pivot), "Pivot must be within the array bounds.");
}
Reverse(array, 0, pivot - 1); // Reverse the first part
Reverse(array, pivot, array.Length - 1); // Reverse the second part
Reverse(array, 0, array.Length - 1); // Reverse the entire array
}
static void Reverse(int[] array, int start, int end)
{
while (start < end)
{
int temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end - ;
}
}
}

Explanation:

  1. Reverse the two parts of the array separately.
  2. Reverse the entire array to get the desired rotation.

Advantages:

  • Avoids additional memory allocation.
  • Performs efficiently in O(n) time.

Performance Considerations

  • Array Slicing: Clean and modern but uses LINQ, which may introduce overhead.
  • Temporary Array: Efficient but requires additional memory for the new array.
  • In-Place Rotation: Most memory-efficient but involves careful implementation.

Conclusion

Rotating an array to a given pivot in C# can be done using multiple methods. Choose the approach that best fits your application’s requirements, whether it’s simplicity, efficiency, or memory usage.

--

--

aamasum
aamasum

Written by aamasum

0 Followers

Net Core | C# | SQL

No responses yet