jayati

Search Insert Position

Nov 23rd, 2022
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. class Solution {
  2. public:
  3.  
  4.    
  5.     int searchInsert(vector<int>& nums, int target) {
  6.         int r=nums.size()-1;
  7.         int l=0;
  8.       while(r>=l) {
  9.                
  10.                int mid = l + (r - l) / 2;
  11.                 if (nums[mid] == target)
  12.                     return mid;
  13.  
  14.                 if (nums[mid] > target)
  15.                     r=mid-1;
  16.  
  17.                 if (nums[mid] < target)
  18.                     l=mid+1;
  19.             }
  20.            
  21.             return l;
  22.        
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment