09. Find Kth Rotation
β GFG solution to the Find Kth Rotation problem: determine how many times a sorted array has been rotated using efficient binary search technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given an increasing sorted rotated array arr[] of distinct integers, find the value of k where the array has been right-rotated k times.
A rotated sorted array is an array that was originally sorted in increasing order but has been rotated (shifted) some number of positions to the right. For example, [1, 2, 3, 4, 5] rotated 2 times becomes [4, 5, 1, 2, 3].
π Examples
Example 1
Input: arr[] = [5, 1, 2, 3, 4]
Output: 1
Explanation: The original sorted array is [1, 2, 3, 4, 5]. The array was rotated 1 time
to the right, resulting in [5, 1, 2, 3, 4].Example 2
Input: arr[] = [1, 2, 3, 4, 5]
Output: 0
Explanation: The given array is not rotated, hence k = 0.Example 3
π Constraints
$1 \le \text{arr.size()} \le 10^5$
$1 \le \text{arr}[i] \le 10^7$
β
My Approach
The optimal approach uses Binary Search to find the index of the minimum element, which represents the number of rotations:
Binary Search for Pivot Point
Key Observation:
In a rotated sorted array, the minimum element is the pivot point.
The index of this minimum element equals the number of rotations.
All elements to the left of pivot are greater than elements to the right.
Initialize Pointers:
Set
left = 0andright = n - 1to represent the search range.
Binary Search Logic:
Calculate middle index:
mid = (left + right) / 2.If
arr[mid] > arr[right]: The minimum is in the right half (including mid+1), so moveleft = mid + 1.Else: The minimum is in the left half (including mid), so move
right = mid.
Termination:
When
left == right, we've found the minimum element's index.Return
leftas the rotation count.
Why This Works:
A sorted array has
arr[0] <= arr[n-1].After rotation, there's a break point where
arr[i] > arr[i+1].Binary search efficiently locates this break point.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(log n), where n is the size of the array. We use binary search which divides the search space in half with each iteration, resulting in logarithmic time complexity.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for storing pointers and the middle index regardless of the input size.
π§βπ» Code (C++)
β‘ View Alternative Approaches with Code and Analysis
π 2οΈβ£ Binary Search with Early Termination
π‘ Algorithm Steps:
Check if array is already sorted by comparing first and last elements.
If sorted, return 0 as there is no rotation.
Use binary search to find the pivot point where rotation occurred.
Compare middle element with its neighbors to determine if it's the minimum.
Adjust search boundaries based on which half is sorted.
π Complexity Analysis:
Time: β±οΈ O(log n) - Binary search with boundary checks
Auxiliary Space: πΎ O(1) - Constant extra space
β
Why This Approach?
Early termination for non-rotated arrays saves computation.
Explicit neighbor checking ensures correctness for edge cases.
Handles circular indexing elegantly.
π 3οΈβ£ Linear Scan Approach
π‘ Algorithm Steps:
Traverse array from start to end sequentially.
Find the position where current element is greater than next element.
This position indicates the end of rotation; next index is the answer.
If no such position exists, array is not rotated, return 0.
π Complexity Analysis:
Time: β±οΈ O(n) - Single traversal of array
Auxiliary Space: πΎ O(1) - No additional space required
β
Why This Approach?
Extremely simple and straightforward logic.
No complex binary search conditions.
Perfect for small arrays or quick prototyping.
π 4οΈβ£ Minimum Value Tracking Approach
π‘ Algorithm Steps:
Initialize variables to track minimum value and its index.
Use binary search to efficiently locate the minimum element.
Compare and update minimum value whenever a smaller element is found.
Return the index of the minimum element as rotation count.
π Complexity Analysis:
Time: β±οΈ O(log n) - Binary search with value tracking
Auxiliary Space: πΎ O(1) - Only tracking variables used
β
Why This Approach?
Explicitly tracks minimum value for clarity.
Robust against edge cases and variations.
Good for understanding the underlying concept.
π 5οΈβ£ Recursive Binary Search
π‘ Algorithm Steps:
Use recursion to divide the problem into smaller subproblems.
Base case checks if current segment has only one element.
Recursively search in the unsorted half of the array.
Return the index where minimum element is located.
π Complexity Analysis:
Time: β±οΈ O(log n) - Recursive binary search
Auxiliary Space: πΎ O(log n) - Recursion call stack
β
Why This Approach?
Clean recursive implementation.
Easier to understand for some programmers.
Demonstrates divide and conquer paradigm.
π π Comparison of Approaches
π Approach
β±οΈ Time Complexity
πΎ Space Complexity
β Pros
β οΈ Cons
π·οΈ Iterative Binary Search
π’ O(log n)
π’ O(1)
π Optimal and clean
π§ Requires understanding BS logic
π― Early Termination
π’ O(log n)
π’ O(1)
β Handles sorted arrays fast
π More code complexity
π Linear Scan
π‘ O(n)
π’ O(1)
π Simplest to understand
π Slower for large arrays
π Minimum Tracking
π’ O(log n)
π’ O(1)
π― Explicit minimum tracking
π§ More variables to manage
π Recursive
π’ O(log n)
π‘ O(log n)
π‘ Clean divide and conquer
πΎ Extra stack space
π Best Choice Recommendation
π― Scenario
ποΈ Recommended Approach
π₯ Performance Rating
π Optimal performance needed
π₯ Iterative Binary Search
β β β β β
π Readability priority
π₯ Linear Scan
β β β ββ
π§ Production code
π₯ Early Termination
β β β β β
π― Interview/Competitive
π Iterative Binary Search
β β β β β
π Learning purposes
π Recursive
β β β β β
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated