An array is first ascending and then descending, using the optimal time complexity to find the maximum value? For example [1,2,2,2,2,3,1]

  java, question

An array is first ascending and then descending to find the maximum value? For example, [1,2,2,2,2,3,1], with the optimal time complexity, the algorithm achieves the maximum value of 3

= = = update
Recursion and array duplication are eliminated, and the return value of the function is changed to index to facilitate the handling of empty arrays.

public class MaxFinder {
 public static void main(String[] args) {
 int[] arr = {2,3,3,3,7,13,22};
 System.out.println(arr[getMaxPos(arr)]);
 bracket
 
 public static int getMaxPos(int[] arr) {
 int len = arr.length;
 if(len >= 2) {
 int min = 0,
 max = len-1;  //index range closed interval of maximum position
 int mid, left;
 while(max-min > 1) {
 Mid = min plus (max-min)/2;
 left = mid-1;
 while(left >= min) {
 if(arr[left] > arr[mid]) {
 max = left;
 break;
 }else if(arr[left] < arr[mid]) {
 min = mid;
 break;
 }else {
 if(left == min) {
 min = mid;
 bracket
 left--;
 bracket
 bracket
 bracket
 return arr[max]>arr[min]?   max : min;
 }else if(len == 1) {
 return 0;
 }else{
 return -1;
 bracket
 bracket
 bracket

= = = original answer

public class Test {
 public static void main(String[] args) {
 int[] arr = {1,2,2,2,2,3,1};
 System.out.println(getMax(arr));
 bracket
 
 public static int getMax(int[] arr) {
 int len = arr.length;
 if(len > 2) {
 int mid = len/2,
 left = mid -1;
 while(left >= 0) {
 if(arr[left] > arr[mid]) {
 Return getmax (arrays.copyofrange (arr, 0, left plus 1));
 }else if(arr[left] < arr[mid]){
 return getMax(Arrays.copyOfRange(arr, mid, arr.length));
 }else{
 left--;
 bracket
 bracket
 // come here only if arr[0] to arr[mid] are all the same
 return getMax(Arrays.copyOfRange(arr, mid, arr.length));
 }else if(len == 2) {
 return arr[0] > arr[1] ?   arr[0] : arr[1];
 }else{
 return arr[0];
 bracket
 bracket
 bracket