Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:26:42+00:00 2026-06-13T01:26:42+00:00

I want to find the maximum element in an array in O(log N) time

  • 0

I want to find the maximum element in an array in O(log N) time using divide and conquer. I found a working code at planet-source-code. I translated it into Python as follows:

def largest(arr,i,j):
    global max1
    global max2
    if i == j:
        max1 = arr[i]
    else:
        if i == j-1:
           max1 = arr[i] if arr[i] > arr[j] else arr[j]
        else:
              mid = (i+j)/2
              largest(arr,i,mid)
              max2 = max1
              largest(arr,mid+1,j)
              if max2 > max1:
             max1 = max2

When I use an array [98,5,4,3,2,76,78,92], and call the code as

max1 = arr[0]
largest(arr,1,8)

I am getting a out-of-bound list index error. However the C code returns the correct result 98. Can anyone spot what error am I doing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:26:43+00:00Added an answer on June 13, 2026 at 1:26 am

    For a general unsorted array, you can never find the max in anything less than O(n) time. The very trivial proof: if you do it in less than O(n) time, then for a sufficiently large array you don’t have enough time to inspect every element. An adversary can therefore put the maximum value in an element you don’t check, making your algorithm incorrect.

    What the original code is good for is finding both the maximum and minimum simultaneously using fewer than 2n comparisons (as the naive implementation would do) — it uses approximately 1.5n comparisons since it performs only one comparison when there are two elements. You derive no benefit from using it to only find the maximum: you’d be better off using max(arr) in Python instead (which would also be faster since it has no function call overhead).

    The original code stores the values in a[1] through a[n], which requires an array of size n+1. Therefore you should put a dummy element in the first position.

    But, more problematically, your translation is incorrect. The original uses globals to effect a multiple value return (which is an incredibly hacky way to do it), and local variables to save the old globals. Since you make both max1 and max2 global, the function will not produce the right answer anyway.

    The correct translation to Python would use a direct multiple value return with a tuple:

    def minmax(arr, i, j):
        if i==j:
            return arr[i], arr[i]
        elif i==j-1:
            if arr[i] < arr[j]:
                return arr[i], arr[j]
            else:
                return arr[j], arr[i]
        else:
            mid = (i+j)//2
            min1, max1 = minmax(arr, i, mid)
            min2, max2 = minmax(arr, mid+1, j)
            if min2 < min1: min1 = min2
            if max2 > max1: max1 = max2
            return min1, max1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is about to find maximum element from an array i want to
How can I find the maximum element and its index from an array in
I want to find the maximum of a nested array, something like this: a
I want to find maximum, minimum and average in an array without .NET in
I have an array of objects. I want to find the maximum of this
I want to find the keys associated with the maximum value in a HashMap<Integer,Integer>
I want to find out the position of an element which is in iframe.
I want to find the first element that has appeared in previous positions in
I have an NSArray of NSNumbers and want to find the maximum value in
I want to find most often seen string in a huge log file. Can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.