多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 在先增加然后减少的数组中找到最大元素 > 原文: [https://www.geeksforgeeks.org/find-the-maximum-element-in-an-array-which-is-first-increasing-and-then-decreasing/](https://www.geeksforgeeks.org/find-the-maximum-element-in-an-array-which-is-first-increasing-and-then-decreasing/) 给定一个整数数组,该数组最初是递增的,然后递减的,请在该数组中找到最大值。 **示例**: ``` Input: arr[] = {8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1} Output: 500 Input: arr[] = {1, 3, 50, 10, 9, 7, 6} Output: 50 Corner case (No decreasing part) Input: arr[] = {10, 20, 30, 40, 50} Output: 50 Corner case (No increasing part) Input: arr[] = {120, 100, 80, 20, 0} Output: 120 ``` **方法 1(线性搜索)** 我们可以遍历数组并跟踪最大值和元素。 最后返回最大元素。 ## C++ ```cpp // C++ program to find maximum  // element  #include <bits/stdc++.h> using namespace std; // function to find the maximum element  int findMaximum(int arr[], int low, int high)  {      int max = arr[low];      int i;      for (i = low + 1; i <= high; i++)      {          if (arr[i] > max)              max = arr[i];          // break when once an element is smaller than          // the max then it will go on decreasing          // and no need to check after that          else             break;      }      return max;  }  /* Driver code*/ int main()  {      int arr[] = {1, 30, 40, 50, 60, 70, 23, 20};      int n = sizeof(arr)/sizeof(arr[0]);      cout << "The maximum element is " << findMaximum(arr, 0, n-1);      return 0;  }  // This is code is contributed by rathbhupendra ```