fork(1) download
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int arr[20], n = 10;
  6.  
  7. int main(){
  8.  
  9. for(int i = 0;i < 10;i++) cin >> arr[i];
  10.  
  11. int start = 0, end = n;
  12. while(start <= end){
  13. int mid = (start + end)/2;
  14. if(mid - 1 >= 0 && mid + 1 <= n && arr[mid-1] >= arr[mid] && arr[mid] >= arr[mid+1]){
  15. //decreasing part
  16. end = mid-1;
  17. }else if(mid - 1 >= 0 && mid + 1 <= n && arr[mid-1] <= arr[mid] && arr[mid] <= arr[mid+1]){
  18. //increasing part
  19. start = mid+1;
  20. }else{
  21. //answer found, take care of corner cases
  22. cout << arr[mid] << endl;
  23. break;
  24. }
  25. }
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0s 3464KB
stdin
3 4 6 8 10 9 6 5 4 2
stdout
10