Largest element in an array's most optimal approach. The code I have written has passed all test cases in https://www.naukri.com/code360/problems/largest-element-in-the-array-largest-element-in-the-array_5026279, and it computes in O(logN) time. Are there any mistakes here? And I cannot find any solutions that exists in LogN time computation. Is there any edge cases to this?
static int largestElement(int[] arr, int n) {
// Write your code here.
int max = arr[0];
int i = 1;
int j = n-1;
while(i<=j){
if(arr[i]>=max && arr[i] >= arr[j]) max = arr[i];
else if(arr[j]>=max && arr[j] >= arr[i]) max = arr[j];
i++;
j--;
}
return max;
}