0

The algorithm described to in this this MIT lecture and written out in this SO question for finding a peak in a 1d array makes sense.

So does its analysis of O(log n); we re dividing the array into halves

How can I update it to find all peaks in the array? What would that complexity be?

Community
  • 1
  • 1
Sam Hammamy
  • 10,819
  • 10
  • 56
  • 94

1 Answers1

3

For finding all peaks, you can't do any better than just going through the whole array and comparing every element to its neighbors. There's no way to tell whether an element you didn't look at is or isn't a peak, so you have to look at all of them.

Thus, the time complexity is O(n) for n elements.

user2357112
  • 260,549
  • 28
  • 431
  • 505