C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# using recursion to find the maximum in a list

By : krackerjax
Date : November 14 2020, 04:51 PM
will be helpful for those in need First off, for the sake of clarity I suggest assigning your list comprehensions to variables so you don't have to write each one twice. This should make the code easier to debug. You can also do the same for the (left+right)//2 value.
code :
``````def max22(L,left,right):
if len(L)==1:
return L[0]
mid = (left+right)//2
left_L = [L[i] for i in range(left, mid)]
right_L = [L[i] for i in range(mid+1, right)]
a = max22(left_L,  0 , len(left_L)-1)
b = max22(right_L, 0 , len(left_L)-1)
return max(a,b)

def max_list22(L):
return max22(L,0,len(L)-1)

print max_list22([4,8,15,16,23,42])
``````
``````def max22(L,left,right):
if len(L)==1:
return L[0]
mid = (left+right+1)//2
left_L = [L[i] for i in range(left, mid)]
right_L = [L[i] for i in range(mid, right+1)]
a = max22(left_L,  0 , len(left_L)-1)
b = max22(right_L, 0 , len(right_L)-1)
return max(a,b)

def max_list22(L):
return max22(L,0,len(L)-1)

print max_list22([4,8,15,16,23,42])
``````
``````def max22(L,left,right):
if len(L)==1:
return L[0]
a = max22([L[i] for i in range(left, (left+right+1)//2)],  0 , len([L[i] for i in range(left, (left+right+1)//2)])-1)
b = max22([L[i] for i in range((left+right+1)//2, right+1)], 0 , len([L[i] for i in range((left+right+1)//2, right+1)])-1)
return max(a,b)

def max_list22(L):
return max22(L,0,len(L)-1)

print max_list22([4,8,15,16,23,42])
``````
``````def max22(L):
if len(L)==1:
return L[0]
mid = (len(L))//2
left_L = [L[i] for i in range(0, mid)]
right_L = [L[i] for i in range(mid, len(L))]
a = max22(left_L)
b = max22(right_L)
return max(a,b)

print max22([4,8,15,16,23,42])
``````

Share :

## Generative recursion to find the sublist with the maximum sum

By : Vanessa Polanco
Date : March 29 2020, 07:55 AM
it helps some times You didn't consider following:
another base case: L is [] left half and right half should be consecutive. According to your code, if L is [2, -5, 3], in the first recursion, left + right will yield 5.
code :
``````def find_max(L):
length = len(L)
mid_index = length/2
if length == 0:
return 0
elif length == 1:
return max(L[0], 0)

left = find_max(L[:mid_index])
right = find_max(L[mid_index:])

left_half = right_half = 0
# to the left
accum = 0
for x in L[mid_index-1::-1]:
accum += x
left_half = max(left_half, accum)

# to the right
accum = 0
for x in L[mid_index:]:
accum += x
right_half = max(right_half, accum)

return max(left, right, left_half + right_half)

assert find_max([]) == 0
assert find_max([-1]) == 0
assert find_max([1, 2, 3]) == 6
assert find_max([2, -5, 3]) == 3
assert find_max([-5, 1, 4, -2, 2, -1, 2, -3, 1, -3, 4]) == 6
``````
``````def sum_max(L, accum=0, max_value=0):
if not L:
return max_value
accum += L[0]
return sum_max(L[1:], accum, max(max_value, accum))

def find_max(L):
...
left_half = sum_max(L[mid_index-1::-1])
right_half = sum_max(L[mid_index:])
...
``````

## Find the maximum elements in an array using recursion

By : Hany
Date : March 29 2020, 07:55 AM
I wish this helpful for you First of all what n.m said is right, copying array is expensive and unnecessary. the other algorithm use start to avoid this.
but for your question. you actually use largest just didnot name it any thing!
code :
``````return findMax(Arrays.copyOf(arr, arr.length-1))> arr[arr.length-1]?findMax(Arrays.copyOf(arr, arr.length-1)):arr[arr.length-1];
``````

## Maximum of a list using recursion?

By : tntinfo
Date : March 29 2020, 07:55 AM
To fix the issue you can do If this isn't a homework question, you should prefer something like this:

## Find maximum value in an array by recursion

By : Sean Bischoff
Date : March 29 2020, 07:55 AM
will help you You are using Divide and Conquer algorithm for finding the maximum element from the array. First, you are dividing the array into individual elements (divide), then you are comparing the elements (conquer). You are dividing the array using calling findMaxHelper recursively.
The general idea of Divide and conquer is shown in the figure:

## Find maximum recursion depth

By : James J
Date : March 29 2020, 07:55 AM
I hope this helps . The only thing that I can think of right now is to use getrlimit to get the maximum size of the stack dedicated to your process. Next thing to do is find a way of getting the currently used stack size. I thought that getrusage is the way to go but after looking at the man-page and a couple of posts on SO it seems that it no longer supports this particular feature. So you have to find another way. I do believe that Valgrind also reports the stack usage so looking into its source code and documentation may prove useful.
Once you are able to get the current stack size you can measure