-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathr.c
More file actions
45 lines (37 loc) · 1.11 KB
/
Copy pathr.c
File metadata and controls
45 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
//using namespace std;
// Returns count of rotations for an array which
// is first sorted in ascending order, then rotated
int countRotations(int arr[], int low, int high)
{
// This condition is needed to handle the case
// when array is not rotated at all
if (high < low)
return 0;
// If there is only one element left
if (high == low)
return low;
// Find mid
int mid = low + (high - low)/2; /*(low + high)/2;*/
// Check if element (mid+1) is minimum element.
// Consider the cases like {3, 4, 5, 1, 2}
if (arr[mid+1] < arr[mid])
return (mid+1);
// Check if mid itself is minimum element
if ( arr[mid] < arr[mid - 1])
return mid;
// Decide whether we need to go to left half or
// right half
if (arr[high] > arr[mid])
return countRotations(arr, low, mid-1);
return countRotations(arr, mid+1, high);
}
// Driver code
int main()
{
int arr[] = {7, 9,11, 13,15,16};
int n = sizeof(arr)/sizeof(arr[0]);
// int p=countRotations(arr,0,n);
printf("%d",countRotations(arr,0,n-1));
return 0;
}