Skip to content
Related Articles

Related Articles

Find bitonic point in given bitonic sequence

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 06 Dec, 2022
Improve Article
Save Article

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++




// C++ program to find bitonic point in a bitonic array.
#include<bits/stdc++.h>
using namespace std;
 
// Function to find bitonic point using binary search
int binarySearch(int arr[], int left, int right)
{
    if (left <= right)
    {
        int mid = (left+right)/2;
 
        // base condition to check if arr[mid] is
        // bitonic point or not
        if (arr[mid-1]<arr[mid] && arr[mid]>arr[mid+1])
            return mid;
 
        // 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] < arr[mid+1])
            return binarySearch(arr, mid+1,right);
        else
            return binarySearch(arr, left, mid-1);
    }
 
    return -1;
}
 
// Driver program to run the case
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




// Java program to find bitonic
// point in a bitonic array.
 
import java.io.*;
 
class GFG
{
    // Function to find bitonic point
    // using binary search
    static int binarySearch(int arr[], int left,
                                       int right)
    {
        if (left <= right)
        {
            int mid = (left + right) / 2;
     
            // base condition to check if arr[mid]
            // is bitonic point or not
            if (arr[mid - 1] < arr[mid] &&
                   arr[mid] > arr[mid + 1])
                   return mid;
     
            // 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] < arr[mid + 1])
                return binarySearch(arr, mid + 1, right);
            else
                return binarySearch(arr, left, mid - 1);
        }
     
        return -1;
    }
     
    // Driver program
    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]);
             
    }
}
 
// This code is contributed by vt_m

Python3




# Python3 program to find bitonic
# point in a bitonic array.
 
# Function to find bitonic point
# using binary search
def binarySearch(arr, left, right):
 
    if (left <= right):
 
        mid = (left + right) // 2;
 
        # base condition to check if
        # arr[mid] is bitonic point
        # or not
        if (arr[mid - 1] < arr[mid] and arr[mid] > arr[mid + 1]):
            return mid;
 
        # 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] < arr[mid + 1]):
            return binarySearch(arr, mid + 1,right);
        else:
            return binarySearch(arr, left, mid - 1);
 
    return -1;
 
# Driver Code
arr = [6, 7, 8, 11, 9, 5, 2, 1];
n = len(arr);
index = binarySearch(arr, 1, n-2);
if (index != -1):
        print(arr[index]);
 
# This code is contributed by mits

C#




// C# program to find bitonic
// point in a bitonic array.
using System;
 
class GFG
{
    // Function to find bitonic point
    // using binary search
    static int binarySearch(int []arr, int left,
                                      int right)
    {
        if (left <= right)
        {
            int mid = (left + right) / 2;
     
            // base condition to check if arr[mid]
            // is bitonic point or not
            if (arr[mid - 1] < arr[mid] &&
                arr[mid] > arr[mid + 1])
                return mid;
     
            // 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] < arr[mid + 1])
                return binarySearch(arr, mid + 1, right);
            else
                return binarySearch(arr, left, mid - 1);
        }
     
        return -1;
    }
     
    // Driver program
    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]);
    }
}
 
// This code is contributed by nitin mittal

PHP




<?php
// PHP program to find bitonic
// point in a bitonic array.
 
// Function to find bitonic point
// using binary search
function binarySearch($arr, $left, $right)
{
    if ($left <= $right)
    {
        $mid = ($left + $right) / 2;
 
        // base condition to check if
        // arr[mid] is bitonic point
        // or not
        if ($arr[$mid - 1] < $arr[$mid] &&
            $arr[$mid] > $arr[$mid + 1])
            return $mid;
 
        // 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] < $arr[$mid + 1])
            return binarySearch($arr, $mid + 1,$right);
        else
            return binarySearch($arr, $left, $mid - 1);
    }
 
    return -1;
}
 
    // Driver Code
    $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];
 
// This code is contributed by nitin mittal
?>

Javascript




<script>
 
// Javascript program to find bitonic
// point in a bitonic array.
 
// Function to find bitonic point
// using binary search
function binarySearch(arr , left,right)
{
    if (left <= right)
    {
        var mid = parseInt((left + right) / 2);
 
        // base condition to check if arr[mid]
        // is bitonic point or not
        if (arr[mid - 1] < arr[mid] &&
               arr[mid] > arr[mid + 1])
               return mid;
 
        // 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] < arr[mid + 1])
            return binarySearch(arr, mid + 1, right);
        else
            return binarySearch(arr, left, mid - 1);
    }
 
    return -1;
}
 
// Driver program
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]);
 
 
// This code is contributed by Amit Katiyar
 
</script>

Output

11

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




// C++ program to find bitonic point in a bitonic array.
#include <bits/stdc++.h>
using namespace std;
 
// Function to find bitonic point using binary search
int binarySearch(int arr[], int low, int high)
{
    // binary search iterative
    while (low <= high)
    {
        // finding the mid index
        int mid = low + (high - low) / 2;
         
        // if both the values on either side
        // of mid index is lesser
        if (arr[mid] > arr[mid - 1]
            and arr[mid] > arr[mid + 1])
            return mid;
         
        // move to the right part
        else if (arr[mid] < arr[mid + 1])
            low = mid + 1;
         
        // move to the left part
        else
            high = mid - 1;
    }
     
    // not found
    return -1;
}
 
// Driver program to run the case
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




/*package whatever //do not write package name here */
import java.io.*;
class GFG {
 
  // Function to find bitonic point using binary search
  static int binarySearch(int arr[], int low, int high)
  {
    // binary search iterative
    while (low <= high)
    {
      // finding the mid index
      int mid = low + (high - low) / 2;
 
      // if both the values on either side
      // of mid index is lesser
      if(arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1])
        return mid;
 
      // move to the right part
      else if (arr[mid] < arr[mid + 1])
        low = mid + 1;
 
      // move to the left part
      else
        high = mid - 1;
    }
 
    // not found
    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]);
  }
}
 
// This code is contributed by aadityaburujwale.

Python3




# python program to find bitonic point in a bitonic array.
# Function to find bitonic point using Binary Search
def binary_search(arr, low, high):
     
    # binary search iterativ
    while(low <= high):
       
        # finding the mid index
        mid = low + (high - low) // 2
         
        # if both the values on either side
        # of mid index is lesser
        if(arr[mid] > arr[mid - 1] and arr[mid] > arr[mid + 1]):
            return mid
        # move to the right part
        elif(arr[mid] < arr[mid + 1]):
            low = mid + 1
             
        # move to the right part
        else:
            high = mid - 1
    # Not Found
    return -1
 
# Driver Code
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])
     
    # This code is contributed by vibhukarnwal077

C#




using System;
 
public class GFG {
 
  // Function to find bitonic point using binary search
  static int binarySearch(int[] arr, int low, int high)
  {
    // binary search iterative
    while (low <= high)
    {
       
      // finding the mid index
      int mid = low + (high - low) / 2;
 
      // if both the values on either side
      // of mid index is lesser
      if (arr[mid] > arr[mid - 1]
          && arr[mid] > arr[mid + 1])
        return mid;
 
      // move to the right part
      else if (arr[mid] < arr[mid + 1])
        low = mid + 1;
 
      // move to the left part
      else
        high = mid - 1;
    }
 
    // not found
    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]);
  }
}
 
// This code is contributed by karandeep1234.

Javascript




   // Function to find bitonic point using binary search
   function binarySearch(arr, low, high)
   {
       // binary search iterative
       while (low <= high)
       {
           // finding the mid index
           var mid = low + parseInt((high - low) / 2);
           // if both the values on either side
           // of mid index is lesser
           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;
           }
       }
        
       // not found
       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]);
       }
 
// This code is contributed by sourabhdalal0001.

Output

11

Time complexity : O(log n)
Auxiliary Space: O(1), as no extra space is required

This article is contributed by Shashank Mishra ( Gullu ). 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. 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!