fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. // Find peak element in the array
  7. int arr[]={2,4,6,8,10,6,5,4};
  8. int end=7,start=0;
  9. while(start<=end){
  10.  
  11. int mid=start+(end-start)/2;
  12.  
  13. if(arr[mid]>arr[mid-1]&& arr[mid]>arr[mid+1]){
  14. cout<<"print the peak element :"<<arr[mid];
  15. return 0;
  16. }else if(arr[mid]>arr[mid-1]){
  17. start=mid+1;
  18. }else{
  19. end=mid-1;
  20. }
  21.  
  22. }
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
print the peak element :10