KONADO
KONADO

Reputation: 189

Largest Difference within a list of integers

I've been trying to write a recursive function which takes a list of numbers such as [5;6;7;8;2;3;4] and returns 3 by finding the largest difference between sequences of ascending numbers in a list but have no idea where to go.

Upvotes: 0

Views: 124

Answers (1)

Stefan Haustein
Stefan Haustein

Reputation: 18803

The function signature should take the lowest number of the current sequence, the highest number of the current sequence and the remainding sequence. If the head of the remaining list is bigger than the highest number of the current sequence, just recurse with the new highest number. Otherwise, return the maximum of the current difference and the recursive call for the remainder with the current head as minimum and maximum.

Upvotes: 1

Related Questions