You are given a bitonic sequence, the task is to find Bitonic Point in it. A Bitonic Sequence is a sequence of numbers which is first strictly increasing then after a point strictly decreasing.
A Bitonic Point is a point in bitonic sequence before which elements are strictly increasing and after which elements are strictly decreasing. A Bitonic point doesn’t exist if array is only decreasing or only increasing.
Examples :
Input : arr[] = {6, 7, 8, 11, 9, 5, 2, 1}
Output: 11
All elements before 11 are smaller and all
elements after 11 are greater.
Input : arr[] = {-3, -2, 4, 6, 10, 8, 7, 1}
Output: 10
Approach 1 : (Recursive Solution)
A simple solution for this problem is to use linear search. Element arr[i] is bitonic point if both i-1’th and i+1’th both elements are less than i’th element. Time complexity for this approach is O(n).
An efficient solution for this problem is to use modified binary search.
If arr[mid-1] < arr[mid] and arr[mid] > arr[mid+1] then we are done with bitonic point.
- If arr[mid] < arr[mid+1] then search in right sub-array, else search in left sub-array.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int binarySearch( int arr[], int left, int right)
{
if (left <= right)
{
int mid = (left+right)/2;
if (arr[mid-1]<arr[mid] && arr[mid]>arr[mid+1])
return mid;
if (arr[mid] < arr[mid+1])
return binarySearch(arr, mid+1,right);
else
return binarySearch(arr, left, mid-1);
}
return -1;
}
int main()
{
int arr[] = {6, 7, 8, 11, 9, 5, 2, 1};
int n = sizeof (arr)/ sizeof (arr[0]);
int index = binarySearch(arr, 1, n-2);
if (index != -1)
cout << arr[index];
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int binarySearch( int arr[], int left,
int right)
{
if (left <= right)
{
int mid = (left + right) / 2 ;
if (arr[mid - 1 ] < arr[mid] &&
arr[mid] > arr[mid + 1 ])
return mid;
if (arr[mid] < arr[mid + 1 ])
return binarySearch(arr, mid + 1 , right);
else
return binarySearch(arr, left, mid - 1 );
}
return - 1 ;
}
public static void main (String[] args)
{
int arr[] = { 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 };
int n = arr.length;
int index = binarySearch(arr, 1 , n - 2 );
if (index != - 1 )
System.out.println ( arr[index]);
}
}
|
Python3
def binarySearch(arr, left, right):
if (left < = right):
mid = (left + right) / / 2 ;
if (arr[mid - 1 ] < arr[mid] and arr[mid] > arr[mid + 1 ]):
return mid;
if (arr[mid] < arr[mid + 1 ]):
return binarySearch(arr, mid + 1 ,right);
else :
return binarySearch(arr, left, mid - 1 );
return - 1 ;
arr = [ 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 ];
n = len (arr);
index = binarySearch(arr, 1 , n - 2 );
if (index ! = - 1 ):
print (arr[index]);
|
C#
using System;
class GFG
{
static int binarySearch( int []arr, int left,
int right)
{
if (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid - 1] < arr[mid] &&
arr[mid] > arr[mid + 1])
return mid;
if (arr[mid] < arr[mid + 1])
return binarySearch(arr, mid + 1, right);
else
return binarySearch(arr, left, mid - 1);
}
return -1;
}
public static void Main ()
{
int []arr = {6, 7, 8, 11, 9, 5, 2, 1};
int n = arr.Length;
int index = binarySearch(arr, 1, n - 2);
if (index != -1)
Console.Write ( arr[index]);
}
}
|
PHP
<?php
function binarySearch( $arr , $left , $right )
{
if ( $left <= $right )
{
$mid = ( $left + $right ) / 2;
if ( $arr [ $mid - 1] < $arr [ $mid ] &&
$arr [ $mid ] > $arr [ $mid + 1])
return $mid ;
if ( $arr [ $mid ] < $arr [ $mid + 1])
return binarySearch( $arr , $mid + 1, $right );
else
return binarySearch( $arr , $left , $mid - 1);
}
return -1;
}
$arr = array (6, 7, 8, 11, 9, 5, 2, 1);
$n = sizeof( $arr );
$index = binarySearch( $arr , 1, $n -2);
if ( $index != -1)
echo $arr [ $index ];
?>
|
Javascript
<script>
function binarySearch(arr , left,right)
{
if (left <= right)
{
var mid = parseInt((left + right) / 2);
if (arr[mid - 1] < arr[mid] &&
arr[mid] > arr[mid + 1])
return mid;
if (arr[mid] < arr[mid + 1])
return binarySearch(arr, mid + 1, right);
else
return binarySearch(arr, left, mid - 1);
}
return -1;
}
var arr = [6, 7, 8, 11, 9, 5, 2, 1];
var n = arr.length;
var index = binarySearch(arr, 1, n - 2);
if (index != -1)
document.write( arr[index]);
</script>
|
Time complexity: O(Log n)
Auxiliary Space: O(1), as no extra space is required
Approach 2 : (Iterative Solution)
Same approach is also followed here, implemented binary search in iterative way.
C++14
#include <bits/stdc++.h>
using namespace std;
int binarySearch( int arr[], int low, int high)
{
while (low <= high)
{
int mid = low + (high - low) / 2;
if (arr[mid] > arr[mid - 1]
and arr[mid] > arr[mid + 1])
return mid;
else if (arr[mid] < arr[mid + 1])
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main()
{
int arr[] = { 6, 7, 8, 11, 9, 5, 2, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
int index = binarySearch(arr, 1, n - 2);
if (index != -1)
cout << arr[index];
return 0;
}
|
Java
import java.io.*;
class GFG {
static int binarySearch( int arr[], int low, int high)
{
while (low <= high)
{
int mid = low + (high - low) / 2 ;
if (arr[mid] > arr[mid - 1 ] && arr[mid] > arr[mid + 1 ])
return mid;
else if (arr[mid] < arr[mid + 1 ])
low = mid + 1 ;
else
high = mid - 1 ;
}
return - 1 ;
}
public static void main (String[] args)
{
int arr[] = { 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 };
int n = arr.length;
int index = binarySearch(arr, 1 , n - 2 );
if (index != - 1 )
System.out.println(arr[index]);
}
}
|
Python3
def binary_search(arr, low, high):
while (low < = high):
mid = low + (high - low) / / 2
if (arr[mid] > arr[mid - 1 ] and arr[mid] > arr[mid + 1 ]):
return mid
elif (arr[mid] < arr[mid + 1 ]):
low = mid + 1
else :
high = mid - 1
return - 1
arr = [ 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 ]
n = len (arr)
idx = binary_search(arr, 1 , n - 2 )
if (idx ! = - 1 ):
print (arr[idx])
|
C#
using System;
public class GFG {
static int binarySearch( int [] arr, int low, int high)
{
while (low <= high)
{
int mid = low + (high - low) / 2;
if (arr[mid] > arr[mid - 1]
&& arr[mid] > arr[mid + 1])
return mid;
else if (arr[mid] < arr[mid + 1])
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
public static void Main( string [] args)
{
int [] arr = { 6, 7, 8, 11, 9, 5, 2, 1 };
int n = arr.Length;
int index = binarySearch(arr, 1, n - 2);
if (index != -1)
Console.WriteLine(arr[index]);
}
}
|
Javascript
function binarySearch(arr, low, high)
{
while (low <= high)
{
var mid = low + parseInt((high - low) / 2);
if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1])
{
return mid;
}
else if (arr[mid] < arr[mid + 1])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
var arr = [6, 7, 8, 11, 9, 5, 2, 1];
var n = arr.length;
var index = binarySearch(arr, 1, n - 2);
if (index != -1)
{
console.log(arr[index]);
}
|
Time complexity : O(log n)
Auxiliary Space: O(1), as no extra space is required
This article is contributed by Shashank Mishra . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.