Posts

Showing posts from August, 2020

[06 Aug 2020] Sorted and Rotated Problems

Image
  Check if an array is sorted and rotated using Binary Search Given an array  arr[]  of N distinct integers, the task is to check if this array is sorted when rotated counter-clockwise. A sorted array is not considered sorted and rotated, i.e., there should at least one rotation. Examples: Input:  arr[] = { 3, 4, 5, 1, 2 } Output:  true Explanation: Sorted array: {1, 2, 3, 4, 5}. Rotating this sorted array clockwise by 3 positions, we get: { 3, 4, 5, 1, 2} Input:  arr[] = {7, 9, 11, 12, 5} Output:  true In this article, an approach using  Binary Search  concept is mentioned. To apply a  binary search , the array needs to follow  some order  by which at every iteration, one half of the array can be eliminated. Therefore, the order followed by an array which is sorted and rotated array is that all the elements to the left of the pivot(the point at which the array is rotated) are in descending order and all the elements to the right of the pivot would be in ascending order. This can be vi