Original function does not work when input array is empty or contains a single value. In the revised code, base case has been augmented to support those. Works successfully for all these test cases: find_max([1, 12, 8, 5]), find_max([]), find_max([1])
10 lines
264 B
Python
10 lines
264 B
Python
def find_max(arr):
|
|
if len(arr) == 0:
|
|
return 0
|
|
elif len(arr) == 1:
|
|
return arr[0]
|
|
elif len(arr) == 2:
|
|
return arr[0] if arr[0] > arr[1] else arr[1]
|
|
sub_max = find_max(arr[1:])
|
|
return arr[0] if arr[0] > sub_max else sub_max
|