Skip to main content
Post Closed as "Needs details or clarity" by πάντα ῥεῖ, toolic, Billal BEGUERADJ
added 5 characters in body
Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221

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. Is Are there any mistakes here. And? 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;
}

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. Is 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;
}

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;
}
Source Link

I wrote a O(log N) code for largest element in an array today but is this an already existing way to find max element in an array?

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. Is 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;
}