📌  相关文章
📜  排序和旋转数组中的最大元素

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

给定在某个未知点旋转的不同元素的排序数组arr[] ,任务是找到其中的最大元素。
例子:

方法:一个简单的解决方案是遍历整个数组并找到最大值。该解决方案需要 O(n) 时间。
我们可以使用二分搜索在 O(Logn) 中完成。如果我们仔细看看上面的例子,我们可以很容易地找出以下模式:

  • 最大元素是唯一下一个小于它的元素。如果没有下一个较小的元素,则没有旋转(最后一个元素是最大值)。我们通过将中间元素与mid – 1mid + 1处的元素进行比较来检查中间元素的这个条件。
  • 如果最大元素不在中间(既不是 mid 也不是 mid + 1),则最大元素位于左半边或右半边。
    1. 如果中间元素大于最后一个元素,则最大元素位于左半部分。
    2. 否则最大元素位于右半部分。

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Function to return the maximum element
int findMax(int arr[], int low, int high)
{
 
     
    if (high == low)
        return arr[low];
 
    // Find mid
    int mid = low + (high - low) / 2;
  // Check if mid reaches 0 ,it is greater than next element or not
     if(mid==0 && arr[mid]>arr[mid+1])
       {
               return arr[mid];
       }
 
    // Check if mid itself is maximum element
    if (mid < high && arr[mid + 1] < arr[mid] && mid>0 && arr[mid]>arr[mid-1]) {
        return arr[mid];
    }
 
    // Decide whether we need to go to
    // the left half or the right half
    if (arr[low] > arr[mid]) {
        return findMax(arr, low, mid - 1);
    }
    else {
        return findMax(arr, mid + 1, high);
    }
}
 
// Driver code
int main()
{
    int arr[] = { 6,5,4,3,2,1};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findMax(arr, 0, n - 1);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to return the maximum element
static int findMax(int arr[], int low, int high)
{
 
 
    // If there is only one element left
    if (high == low)
        return arr[low];
 
    // Find mid
    int mid = low + (high - low) / 2;
  // Check if mid reaches 0 ,it is greater than next element or not
  if(mid==0 && arr[mid]>arr[mid+1])
  {
    return arr[mid];
  }
 
 
    // Decide whether we need to go to
    // the left half or the right half
    if (arr[low] > arr[mid])
    {
        return findMax(arr, low, mid - 1);
    }
    else
    {
        return findMax(arr, mid + 1, high);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 6,5,4,3,2,1 };
    int n = arr.length;
    System.out.println(findMax(arr, 0, n - 1));
}
}


Python3
# Python3 implementation of the approach
 
# Function to return the maximum element
def findMax(arr, low, high):
 
    # If there is only one element left
    if (high == low):
        return arr[low]
 
    # Find mid
    mid = low + (high - low) // 2
    # Check if mid reaches 0 ,it is greater than next element or not
    if(mid==0 and arr[mid]>arr[mid+1]):
          return arr[mid]
 
    # Check if mid itself is maximum element
    if (mid < high and arr[mid + 1] < arr[mid] and mid>0 and arr[mid]>arr[mid-1]):
        return arr[mid]
     
   
 
    # Decide whether we need to go to
    # the left half or the right half
    if (arr[low] > arr[mid]):
        return findMax(arr, low, mid - 1)
    else:
        return findMax(arr, mid + 1, high)
 
# Driver code
arr = [6,5,4,3,2,1]
n = len(arr)
print(findMax(arr, 0, n - 1))


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the maximum element
static int findMax(int []arr,
                   int low, int high)
{
 
    
    // If there is only one element left
    if (high == low)
        return arr[low];
     
  
 
    // Find mid
    int mid = low + (high - low) / 2;
    // Check if mid reaches 0 ,it is greater than next element or not
    if(mid==0 && arr[mid]>arr[mid+1])
        return arr[mid];
 
    // Check if mid itself is maximum element
    if (mid < high && arr[mid + 1] < arr[mid] && mid>0 && arr[mid]>arr[mid-1])
    {
        return arr[mid];
    }
 
    // Decide whether we need to go to
    // the left half or the right half
    if (arr[low] > arr[mid])
    {
        return findMax(arr, low, mid - 1);
    }
    else
    {
        return findMax(arr, mid + 1, high);
    }
}
 
// Driver code
public static void Main()
{
    int []arr = { 6,5, 1, 2, 3, 4 };
    int n = arr.Length;
     
    Console.WriteLine(findMax(arr, 0, n - 1));
}
}


PHP
$arr[$mid-1])
          return $arr[0];
 
    // Check if mid itself is maximum element
    if ($mid < $high && $arr[$mid + 1] < $arr[$mid] && $mid > 0 && $arr[$mid]>$arr[$mid-1])
    {
        return $arr[$mid];
    }
    // Decide whether we need to go to
    // the left half or the right half
    if ($arr[$low] > $arr[$mid])
    {
        return findMax($arr, $low, $mid - 1);
    }
    else
    {
        return findMax($arr, $mid + 1, $high);
    }
}
 
// Driver code
$arr = array(5,6,1,2,3,4);
$n = sizeof($arr);
echo findMax($arr, 0, $n - 1);


Javascript


输出:
6

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