fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int search_insert_position(int arr[],int n,int target)
  5. {
  6. int start=0;
  7. int end=n-1;
  8. int mid=start+(end-start)/2;
  9. while(start<=end)
  10. {
  11. if(arr[mid]==target)
  12. {
  13. return mid;
  14. }
  15. else if(arr[mid]<target)
  16. {
  17. start=mid+1;
  18. }
  19. else
  20. {
  21. end=mid-1;
  22. }
  23. mid=start+(end-start)/2;
  24. }
  25. return start;
  26. }
  27. int main()
  28. {
  29. int arr[100],size,target;
  30. cout<<"Enter the size of an array:";
  31. cin>>size;
  32. cout<<"Enter the array elements:";
  33. for(int i=0;i<size;i++)
  34. {
  35. cin>>arr[i];
  36. }
  37. cout<<"Enter the target value:";
  38. cin>>target;
  39. search_insert_position(arr,size,target);
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5304KB
stdin
45
stdout
Enter the size of an array:Enter the array elements:Enter the target value: