fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. // Function to find peak element of the array
  6. int findPeakElement(std::vector<int> data)
  7. {
  8. auto peak = std::adjacent_find(data.begin(), data.end(), std::greater<>());
  9. if (peak == data.end())
  10. {
  11. // to handle when array is sorted in ascending order,
  12. // re-position to last element
  13. --peak;
  14. }
  15. return *peak;
  16. }
  17.  
  18. // main function
  19. int main()
  20. {
  21. std::vector<int> data { 6, 8, 9, 14, 10, 12};
  22.  
  23. std::cout << "The peak element is " << findPeakElement(data);
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0s 4436KB
stdin
Standard input is empty
stdout
The peak element is 14