fork(1) download
  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4.  
  5. // find right position in the array
  6. cout<<"Enter the target\n";
  7. int target;
  8. cin>>target;
  9. int arr[8]={1,4,6,8,10,14,16,18};
  10. int start=0,end=7;
  11. int pos=-1;
  12. while(start<=end){
  13. int mid=start+(end-start)/2;
  14. if(arr[mid]==target){
  15. pos=mid;
  16. }else if(arr[mid]<target){
  17. start=mid+1;
  18. }else{
  19. pos=mid;
  20. end=mid-1;
  21. }
  22. }
  23.  
  24. cout<<pos;
  25. return 0;
  26. }
Success #stdin #stdout 0s 5308KB
stdin
9
stdout
Enter the target
4