I'm trying to figure out how to write a cascade recursive function to find a max element in an integer array. I've tried to write some code but i think my function is more linear than cascade.
Here is my pascal code. vector = array of integer, l and r are used as indices, at first l = 0 and r = size of v - 1. I've tried to write this code without an auxiliary variable for maximum, however, i'm not sure if it is possible to do this task in a cascade way without such a variable.
function cascademax(v: vector; l, r: integer): integer;
begin
if r = 0 then
result := v[0]
else
begin
if (l < 0) or (r < 0) or (l > r) then
result := -1
else
begin
if l = r then
begin
if v[l] >= v[r] then
result := v[l]
else
result := v[r]
end
else
begin
if v[l] >= v[r] then
result := cascademax(v, l, r - 1)
else
result := cascademax(v, l + 1, r)
end;
end;
end;
end;
I will be glad if you write your code in any language or at least tell me if i need an auxiliary variable or not