给定一个不同元素的排序数组arr [] ,该数组在某个未知点旋转,任务是在其中找到最大元素。
例子:
Input: arr[] = {3, 4, 5, 1, 2}
Output: 5
Input: arr[] = {1, 2, 3}
Output: 3
方法:一个简单的解决方案是遍历整个数组并找到最大值。该解决方案需要O(n)时间。
我们可以使用Binary Search在O(Logn)中进行操作。如果我们仔细看一下上面的示例,我们可以很容易地找出以下模式:
- 最大元素是唯一一个其下一个小于它的元素。如果没有下一个较小的元素,则不会旋转(最后一个元素为最大值)。我们通过与中– 1和中+ 1的元素进行比较来检查此条件是否存在中间元素。
- 如果最大元素不在中间(既不是中也不是中+1),则最大元素位于左半部分或右半部分。
- 如果中间元素大于最后一个元素,则最大元素位于左半部分。
- 其他最大元素位于右半部分。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum element
int findMax(int arr[], int low, int high)
{
// This condition is for the case when
// array is not rotated at all
if (high < low)
return arr[0];
// If there is only one element left
if (high == low)
return arr[low];
// Find mid
int mid = low + (high - low) / 2;
// Check if mid itself is maximum element
if (mid < high && arr[mid + 1] < arr[mid]) {
return arr[mid];
}
// Check if element at (mid - 1) is maximum element
// Consider the cases like {4, 5, 1, 2, 3}
if (mid > low && arr[mid] < arr[mid - 1]) {
return arr[mid - 1];
}
// 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[] = { 5, 6, 1, 2, 3, 4 };
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)
{
// This condition is for the case when
// array is not rotated at all
if (high < low)
return arr[0];
// If there is only one element left
if (high == low)
return arr[low];
// Find mid
int mid = low + (high - low) / 2;
// Check if mid itself is maximum element
if (mid < high && arr[mid + 1] < arr[mid])
{
return arr[mid];
}
// Check if element at (mid - 1) is maximum element
// Consider the cases like {4, 5, 1, 2, 3}
if (mid > low && arr[mid] < arr[mid - 1])
{
return arr[mid - 1];
}
// 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[] = { 5, 6, 1, 2, 3, 4 };
int n = arr.length;
System.out.println(findMax(arr, 0, n - 1));
}
}
// This code is contributed by Code_Mech.
Python3
# Python3 implementation of the approach
# Function to return the maximum element
def findMax(arr, low, high):
# This condition is for the case when
# array is not rotated at all
if (high < low):
return arr[0]
# If there is only one element left
if (high == low):
return arr[low]
# Find mid
mid = low + (high - low) // 2
# Check if mid itself is maximum element
if (mid < high and arr[mid + 1] < arr[mid]):
return arr[mid]
# Check if element at (mid - 1) is maximum element
# Consider the cases like {4, 5, 1, 2, 3}
if (mid > low and arr[mid] < arr[mid - 1]):
return arr[mid - 1]
# 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 = [5, 6, 1, 2, 3, 4]
n = len(arr)
print(findMax(arr, 0, n - 1))
# This code is contributed by mohit kumar
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)
{
// This condition is for the case
// when array is not rotated at all
if (high < low)
return arr[0];
// If there is only one element left
if (high == low)
return arr[low];
// Find mid
int mid = low + (high - low) / 2;
// Check if mid itself is maximum element
if (mid < high && arr[mid + 1] < arr[mid])
{
return arr[mid];
}
// Check if element at (mid - 1)
// is maximum element
// Consider the cases like {4, 5, 1, 2, 3}
if (mid > low && arr[mid] < arr[mid - 1])
{
return arr[mid - 1];
}
// 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 = { 5, 6, 1, 2, 3, 4 };
int n = arr.Length;
Console.WriteLine(findMax(arr, 0, n - 1));
}
}
// This code is contributed by Ryuga
PHP
$low && $arr[$mid] < $arr[$mid - 1])
{
return $arr[$mid - 1];
}
// 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);
// This code is contributed
// by Akanksha Rai
Javascript
输出:
6
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。