📌  相关文章
📜  在给定的音调序列中找到音调点

📅  最后修改于: 2021-04-26 10:18:13             🧑  作者: 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 + 1],则我们完成了双音点。
  • 如果arr [mid] 则在右子数组中搜索,否则在左子数组中搜索。
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