forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotationCount.py
More file actions
63 lines (45 loc) · 1.42 KB
/
RotationCount.py
File metadata and controls
63 lines (45 loc) · 1.42 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''
Descrption ->
Given an array of distinct number enteries which are arranged in ascending order.
If the array is rotated clockwise number of times. Then, calculate the total
number of rotations in the respective array.
'''
# program to find number of rotations in a sorted rotated array.
# PYTHON CODE:
def Total(lst,low, high):
# When array in not rotated at all
if (high < low):
return 0
# When array has single element left
if (high == low):
return low
# calculating middle value
mid = low + (high - low)/2;
mid = int(mid)
# checking minimum element
if (mid < high and lst[mid+1] < lst[mid]):
return (mid+1)
#check if mid is middle element
if (mid > low and lst[mid] < lst[mid - 1]):
return mid
#Move either left or right
if (lst[high] > lst[mid]):
return Total(lst, low, mid-1);
return Total(lst, mid+1, high)
# main code
lst = list(map(int,input().strip().split()))
n = len(lst)
print(Total(lst, 0, n-1))
'''
Test Case 1:
Input : lst = [4, 9, 11, 12, 5]
Output : 4
Test Case 2:
Input : lst = [7, 9, 11, 12, 15]
Output : 0
Test Case 3:
Input : lst = [15, 18, 2, 3, 6, 12]
Output : 2
Time Complexity: O(log n) # n = total number of elements in the array
Space Complexity: O(1)
'''