 <p>Here's the pseudo-code for doing this</p> <p>input: listOfNumbers</p> <pre><code>//Handle exceptional cases if listOfNumbers.length == 0 return [] if listOfNumbers.length == 1 return [listOfNumbers[0]] //Pre-condition listOfNumbers.length &gt; 1 extremes = emptyList lastNumber = listOfNumbers[0] isIncreasing = listOfNumbers[0] &lt; listOfNumbers[1] extremes.push(listOfNumbers[0]) foreach number in listOfNumbers[1...listOfNumbers.length] if(isIncreasing AND lastNumber &gt; number) extremes.push(lastNumber) isIncreasing = false if(NOT isIncreasing AND lastNumber &lt; number) extremes.push(lastNumber) isIncreasing = true extremes.push(listOfNumbers.length-1) return extremes </code></pre> <p>I think this would do it, although I haven't tested this.</p>