Peak Finding

You are given an array of numbers which increase up to a point and then decreases until the end of the array. Find the position where the array stops increasing any further and store it in a variable named peak.

Example

Input:

let numbers = [1, 2, 3, 5, 9, 4, 3, 2, 1]

Expected value:

peak = 4

[collapse]
Hint

The maximum number in the array will always be the peak.

[collapse]
Solution

let numbers = [1, 2, 3, 5, 9, 4, 3, 2, 1]

let maxNumber = numbers.max()!
let peak = numbers.index(of: maxNumber)!

[collapse]
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
Subscribe
We send about one email per week with our latest tutorials and updates
Never display this again :)