forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitonic_Point.rb
51 lines (47 loc) · 1.24 KB
/
Bitonic_Point.rb
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
=begin
BITONIC POINT
Given an array. The task is to find the bitonic point of the array.
The bitonic point in an array is the index before which all the numbers
are in increasing order and after which, all are in decreasing order.
=end
def bitonic(arr, left_end, right_end)
if left_end <= right_end
mid_point = (left_end + right_end) / 2
if arr[mid_point - 1] < arr[mid_point] && arr[mid_point] > arr[mid_point + 1]
return mid_point
end
# We assume that sequence is bitonic. We go to
# right subarray if middle point is part of
# increasing subsequence. Else we go to left
# subarray.
if arr[mid_point] < arr[mid_point + 1]
return bitonic(arr, mid_point + 1, right_end)
else
return bitonic(arr, left_end, mid_point - 1)
end
end
return -1
end
print("Enter the number of elements : ")
n = gets.chomp.to_i
arr = []
for i in 0..n-1
arr.push(gets.chomp.to_i)
end
length = arr.length()
# Driver Function
point = bitonic(arr, 1, length - 2)
if point != -1
print("The bitonic index is " + point.to_s)
else
print("There is no bitonic point")
end
# output
# Enter the number of elements : 6
# 1
# 3
# 5
# 9
# 4
# 2
# The bitonic index is 3