-
Notifications
You must be signed in to change notification settings - Fork 617
/
peak_element.py
71 lines (59 loc) · 1.8 KB
/
peak_element.py
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
64
65
66
67
68
69
70
71
'''
Peak Element using Binary Search
Problem Statement: Given an array of size n, find the peak element.
Peak element is the element which is greater than its neighbours.
There may exist more than one peak element, so print any one.
'''
def binary_search(in_arr, start, end):
# find the middle element useing "start + (end - start) / 2"
mid = start + (end - start) // 2
# check if the middle element is greater than its neighbors
# by applying the condition for binary search
if ((mid == 0 or in_arr[mid - 1] <= in_arr[mid]) and
(mid == len(in_arr) - 1 or in_arr[mid + 1] <= in_arr[mid])):
# if true then return the index of that element
return mid
# If the left neighbor of "mid" is greater than the middle element,
# find the peak in the left sublist using recursion
if mid - 1 >= 0 and in_arr[mid - 1] > in_arr[mid]:
return binary_search(in_arr, start, mid - 1)
# If the right neighbor of "mid" is greater than the middle element,
# find the peak in the right sublist using recursion
return binary_search(in_arr, mid + 1, end)
if __name__ == '__main__':
# input the size of array
n = int(input())
arr = []
# enter the elements of the array
for i in range(0, n):
arr.append(int(input()))
# calling the function to find the index of peak element
pos = binary_search(arr, 0, n - 1)
# printing the element
print("The peak element is", arr[pos])
'''
Test Case 1:
Input -
5
1
3
2
5
1
Output -
The peak element is 3
Test Case 2:
Input -
7
1
2
3
4
3
2
1
Output -
The peak element is 4
Time Complexity: O(logn), since we used binary search
Space Complexity: O(1), no extra space is used
'''