📜  在给定的双音序列中找到双音点

📅  最后修改于: 2021-09-16 11:09:58             🧑  作者: Mango

给你一个双音序列,任务是在其中找到双音点。双调序列是一个数字序列,它首先严格增加,然后在一个点之后严格减少。
双调点是双调序列中的一个点,在该点之前元素严格增加,之后元素严格减少。如果数组仅减少或仅增加,则双调点不存在。
例子 :

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

这个问题的一个简单解决方案是使用线性搜索。如果第 i-1 个和第 i+1 个元素都小于第 i 个元素,则元素arr[i]是双调点。这种方法的时间复杂度是 O(n)。
此问题的有效解决方案是使用修改后的二分查找

  • 如果arr[mid-1] < arr[mid]arr[mid] > arr[mid+1]那么我们就完成了双调点。
  • 如果arr[mid] < arr[mid+1]则在右子数组中搜索,否则在左子数组中搜索。
C++
// C++ program to find bitonic point in a bitonic array.
#include
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+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
 $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


输出:

11

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程