-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathMax_in_BitonicArray.cpp
84 lines (71 loc) · 2.5 KB
/
Max_in_BitonicArray.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
/*
Find the maximum element in Bitonic array
==========================================
A bitonic array is an array of integers which is initially
increasing and then decreasing.
An efficient solution to find the maximum element
in bitonic array is by using binary search.
Given an array of integers which is initially increasing
and then decreasing, find the maximum value in the array.
*/
#include<bits/stdc++.h>
using namespace std;
/* This function is used to find maximum
element in bitonic array
*/
int findMax(int arr[],int low,int high){
/* Base Case: Only one element is present in arr[low..high]*/
if (low == high)
return arr[low];
/* If there are two elements and first is greater then
the first element is maximum */
if ((high == low + 1) && arr[low] >= arr[high])
return arr[low];
/* If there are two elements and second is greater then
the second element is maximum */
if ((high == low + 1) && arr[low] < arr[high])
return arr[high];
int mid = (low + high)/2; /*low + (high - low)/2;*/
/* If we reach a point where arr[mid] is greater than both of
its adjacent elements arr[mid-1] and arr[mid+1], then arr[mid]
is the maximum element*/
if ( arr[mid] > arr[mid + 1] && arr[mid] > arr[mid - 1])
return arr[mid];
/* If arr[mid] is greater than the next element and smaller than the previous
element then maximum lies on left side of mid */
if (arr[mid] > arr[mid + 1] && arr[mid] < arr[mid - 1])
return findMax(arr, low, mid-1);
else /* when arr[mid] is greater than arr[mid-1] and smaller than arr[mid+1] */
return findMax(arr, mid + 1, high);
}
//Drivers' Code
int main()
{
int size;
cout<<"Enter the size of the Array:";
cin>>size;
int arr[size]; //declaring the array
cout<<"Enter the elements of the Array:";
for(int i=0;i<size;i++)
cin>>arr[i];
cout<<"The maximum element is: "<<findMax(arr,0,size-1)<<endl;
return 0;
}
/*
Time Complexity: O(Logn)
Auxilary Space Complexity:O(1)
Testcase '1':
Enter the size of the Array:11
Enter the elements of the Array:8 10 20 80 100 200 400 500 3 2 1
The maximum element is: 500
Testcase '2':
Corner case (No decreasing part)
Enter the size of the Array:5
Enter the elements of the Array:10 20 30 40 50
The maximum element is: 50
Testcase '3':
Corner case (No increasing part)
Enter the size of the Array:5
Enter the elements of the Array:120 100 80 20 0
The maximum element is: 120
*/