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.
Note:- Given Sequence will always be a valid bitonic sequence.
Examples:
Input: arr[] = {8, 10, 100, 200, 400, 500, 3, 2, 1}
Output: 500
Input: arr[] = {10, 20, 30, 40, 30, 20}
Output: 40
Input: arr[] = {60, 70, 120, 100, 80}
Output: 120
Table of Content
[Naive Approach] Using Linear Search - O(n) Time and O(1) Space
A simple approach is to iterate through the array and keep track of the maximum element occurred so far. once the traversal is complete, return the maximum element.
// C++ program to find maximum element in bitonic
// array using linear search
#include <iostream>
#include <vector>
using namespace std;
int bitonicPoint(vector<int> &arr) {
int res = arr[0];
// Traverse the array to find
// the maximum element
for (int i = 1; i < arr.size(); i++)
res = max(res, arr[i]);
return res;
}
int main() {
vector<int> arr = {8, 10, 100, 400, 500, 3, 2, 1};
cout << bitonicPoint(arr);
return 0;
}
// C program to find maximum element in bitonic
// array using linear search
#include <stdio.h>
int bitonicPoint(int arr[], int n) {
int res = arr[0];
// Traverse the array to find
// the maximum element
for (int i = 1; i < n; i++)
res = (res > arr[i]) ? res : arr[i];
return res;
}
int main() {
int arr[] = {8, 10, 100, 400, 500, 3, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", bitonicPoint(arr, n));
return 0;
}
// Java program to find maximum element in bitonic
// array using linear search
import java.util.Arrays;
class GfG {
static int bitonicPoint(int[] arr) {
int res = arr[0];
// Traverse the array to find
// the maximum element
for (int i = 1; i < arr.length; i++)
res = Math.max(res, arr[i]);
return res;
}
public static void main(String[] args) {
int[] arr = {8, 10, 100, 400, 500, 3, 2, 1};
System.out.println(bitonicPoint(arr));
}
}
# Python program to find maximum element in
# bitonic array using linear search
def bitonicPoint(arr):
res = arr[0]
# Traverse the array to find
# the maximum element
for i in range(1, len(arr)):
res = max(res, arr[i])
return res
if __name__ == "__main__":
arr = [8, 10, 100, 400, 500, 3, 2, 1]
print(bitonicPoint(arr))
// C# program to find maximum element in bitonic
// array using linear search
using System;
class GfG {
static int bitonicPoint(int[] arr) {
int res = arr[0];
// Traverse the array to find
// the maximum element
for (int i = 1; i < arr.Length; i++)
res = Math.Max(res, arr[i]);
return res;
}
static void Main() {
int[] arr = {8, 10, 100, 400, 500, 3, 2, 1};
Console.WriteLine(bitonicPoint(arr));
}
}
// JavaScript program to find maximum element in
// bitonic array using linear search
function bitonicPoint(arr) {
let res = arr[0];
// Traverse the array to find
// the maximum element
for (let i = 1; i < arr.length; i++)
res = Math.max(res, arr[i]);
return res;
}
const arr = [8, 10, 100, 400, 500, 3, 2, 1];
console.log(bitonicPoint(arr));
Output
500
[Expected Approach] Using Binary Search - O(logn) Time and O(1) Space
The input array follows a monotonic pattern. If an element is smaller than the next one, it lies in the increasing segment of the array, and the maximum element will definitely exist after it. Conversely, if an element is greater than the next one, it lies in the decreasing segment, meaning the maximum is either at this position or earlier. Therefore, we can use binary search to efficiently find the maximum element in the array.
// C++ program to find the maximum element in a bitonic
// array using binary search.
#include <iostream>
#include <vector>
using namespace std;
int bitonicPoint(vector<int> &arr) {
int n = arr.size();
// Search space for binary search.
int lo = 0, hi = n - 1;
int res = n - 1;
while(lo <= hi) {
int mid = (lo + hi) / 2;
// Decreasing segment
if(mid + 1 < n && arr[mid] > arr[mid + 1]) {
res = mid;
hi = mid - 1;
}
// Increasing segment
else {
lo = mid + 1;
}
}
return arr[res];
}
int main() {
vector<int> arr = {8, 10, 100, 400, 500, 3, 2, 1};
cout << bitonicPoint(arr);
return 0;
}
// C program to find the maximum element in a bitonic
// array using binary search.
#include <stdio.h>
int bitonicPoint(int arr[], int n) {
// Search space for binary search.
int lo = 0, hi = n - 1;
int res = hi;
while(lo <= hi) {
int mid = (lo + hi) / 2;
// Decreasing segment
if(mid + 1 < n && arr[mid] > arr[mid + 1]) {
res = mid;
hi = mid - 1;
}
// Increasing segment
else {
lo = mid + 1;
}
}
return arr[res];
}
int main() {
int arr[] = {8, 10, 100, 400, 500, 3, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", bitonicPoint(arr, n));
return 0;
}
// Java program to find the maximum element in a bitonic
// array using binary search.
import java.util.Arrays;
class GfG {
static int bitonicPoint(int[] arr) {
int n = arr.length;
// Search space for binary search.
int lo = 0, hi = n - 1;
int res = n - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
// Decreasing segment
if (mid + 1 < n && arr[mid] > arr[mid + 1]) {
res = mid;
hi = mid - 1;
}
// Increasing segment
else {
lo = mid + 1;
}
}
return arr[res];
}
public static void main(String[] args) {
int[] arr = {8, 10, 100, 400, 500, 3, 2, 1};
System.out.println(bitonicPoint(arr));
}
}
# Python program to find the maximum element in a bitonic
# array using binary search.
def bitonicPoint(arr):
# Search space for binary search.
lo = 0
hi = len(arr) - 1
res = hi
while lo <= hi:
mid = (lo + hi) // 2
# Decreasing segment
if mid + 1 < len(arr) and arr[mid] > arr[mid + 1]:
res = mid
hi = mid - 1
# Increasing segment
else:
lo = mid + 1
return arr[res]
if __name__ == "__main__":
arr = [8, 10, 100, 400, 500, 3, 2, 1]
print(bitonicPoint(arr))
// C# program to find the maximum element in a bitonic
// array using binary search.
using System;
class GfG {
static int bitonicPoint(int[] arr) {
int n = arr.Length;
// Search space for binary search.
int lo = 0, hi = n - 1;
int res = n - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
// Decreasing segment
if (mid + 1 < n && arr[mid] > arr[mid + 1]) {
res = mid;
hi = mid - 1;
}
// Increasing segment
else {
lo = mid + 1;
}
}
return arr[res];
}
static void Main() {
int[] arr = {8, 10, 100, 400, 500, 3, 2, 1};
Console.WriteLine(bitonicPoint(arr));
}
}
// JavaScript program to find the maximum element in a bitonic
// array using binary search.
function bitonicPoint(arr) {
const n = arr.length;
// Search space for binary search.
let lo = 0, hi = n - 1;
let res = n - 1;
while (lo <= hi) {
let mid = Math.floor((lo + hi) / 2);
// Decreasing segment
if (mid + 1 < n && arr[mid] > arr[mid + 1]) {
res = mid;
hi = mid - 1;
}
// Increasing segment
else {
lo = mid + 1;
}
}
return arr[res];
}
const arr = [8, 10, 100, 400, 500, 3, 2, 1];
console.log(bitonicPoint(arr));
Output
500