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 find the number of times array is rotated in the sorted array by recursion using C#?
To find the number of times a sorted array has been rotated, we need to locate the position of the minimum element. The number of rotations equals the index of the minimum element. We can solve this efficiently using binary search recursion.
In a rotated sorted array, the minimum element is the pivot point where the rotation occurred. For example, in array [4, 5, 6, 1, 2, 3], the minimum element 1 is at index 3, meaning the array was rotated 3 times.
Algorithm
The recursive binary search approach works as follows −
-
Find the middle element and compare it with the start and end elements.
-
If the left half is sorted (arr[start] ? arr[mid]), the minimum is in the right half.
-
If the right half is sorted (arr[mid] ? arr[end]), the minimum is in the left half.
-
Continue recursively until we find the minimum element.
Finding Rotation Count
Example
using System;
public class RotatedArray {
public int FindRotationCount(int[] arr, int start, int end) {
if (start > end) return 0;
// If array is not rotated
if (start == end) return start;
int mid = start + (end - start) / 2;
// Check if mid+1 is the minimum element
if (mid < end && arr[mid] > arr[mid + 1]) {
return mid + 1;
}
// Check if mid is the minimum element
if (mid > start && arr[mid] < arr[mid - 1]) {
return mid;
}
// If left half is sorted, minimum is in right half
if (arr[end] > arr[mid]) {
return FindRotationCount(arr, start, mid - 1);
}
// Otherwise, minimum is in left half
return FindRotationCount(arr, mid + 1, end);
}
}
class Program {
static void Main(string[] args) {
RotatedArray ra = new RotatedArray();
int[] arr1 = { 4, 5, 6, 7, 1, 2, 3 };
int[] arr2 = { 6, 7, 1, 2, 3, 4, 5 };
int[] arr3 = { 1, 2, 3, 4, 5, 6, 7 };
Console.WriteLine("Array: [4, 5, 6, 7, 1, 2, 3]");
Console.WriteLine("Rotation count: " + ra.FindRotationCount(arr1, 0, arr1.Length - 1));
Console.WriteLine("Array: [6, 7, 1, 2, 3, 4, 5]");
Console.WriteLine("Rotation count: " + ra.FindRotationCount(arr2, 0, arr2.Length - 1));
Console.WriteLine("Array: [1, 2, 3, 4, 5, 6, 7]");
Console.WriteLine("Rotation count: " + ra.FindRotationCount(arr3, 0, arr3.Length - 1));
}
}
The output of the above code is −
Array: [4, 5, 6, 7, 1, 2, 3] Rotation count: 4 Array: [6, 7, 1, 2, 3, 4, 5] Rotation count: 2 Array: [1, 2, 3, 4, 5, 6, 7] Rotation count: 0
Searching in Rotated Array
Once we understand rotation count, we can also search for elements in a rotated sorted array using similar logic −
Example
using System;
public class RotatedArraySearch {
public int SearchInRotatedArray(int[] arr, int start, int end, int target) {
if (start > end) return -1;
int mid = start + (end - start) / 2;
if (arr[mid] == target) return mid;
// Check which half is sorted
if (arr[start] <= arr[mid]) {
// Left half is sorted
if (target >= arr[start] && target < arr[mid]) {
return SearchInRotatedArray(arr, start, mid - 1, target);
}
return SearchInRotatedArray(arr, mid + 1, end, target);
} else {
// Right half is sorted
if (target > arr[mid] && target <= arr[end]) {
return SearchInRotatedArray(arr, mid + 1, end, target);
}
return SearchInRotatedArray(arr, start, mid - 1, target);
}
}
}
class Program {
static void Main(string[] args) {
RotatedArraySearch ras = new RotatedArraySearch();
int[] arr = { 4, 5, 6, 7, 1, 2, 3 };
Console.WriteLine("Array: [4, 5, 6, 7, 1, 2, 3]");
Console.WriteLine("Search for 1: Index " + ras.SearchInRotatedArray(arr, 0, arr.Length - 1, 1));
Console.WriteLine("Search for 6: Index " + ras.SearchInRotatedArray(arr, 0, arr.Length - 1, 6));
Console.WriteLine("Search for 9: Index " + ras.SearchInRotatedArray(arr, 0, arr.Length - 1, 9));
}
}
The output of the above code is −
Array: [4, 5, 6, 7, 1, 2, 3] Search for 1: Index 4 Search for 6: Index 2 Search for 9: Index -1
Time and Space Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Finding Rotation Count | O(log n) | O(log n) due to recursion |
| Searching in Rotated Array | O(log n) | O(log n) due to recursion |
Conclusion
Finding the rotation count in a sorted rotated array involves locating the minimum element using binary search recursion. The index of the minimum element gives us the exact number of rotations. This approach works in O(log n) time complexity, making it efficient for large arrays.
