CowEatsGrass - 10 months ago 52

C++ Question

Suppose I have the following declarations:

`int arr[5] = {1,10,9,28,3};`

int low = 0;

int high = 4;

int largest = findLargest(ar, low, high);

I am suppose to write a "findLargest" function using recursion, and this is what I got

`int findLargest(int arr[], int low, int high)`

{

if (low == high)

return arr[low];

return max(arr[low], findLargest(arr, low+1, high));

}

The output was

`28`

`>`

`<`

`==`

Answer Source

The recursion is using the `std::max`

function, which uses `operator>`

. The maximum of the subarray `[a;b]`

is the maximum between `a`

and the maximum of the subarray `[a + 1;b]`

(Which is `a`

if `a = b`

).