Solution
class Solution { public: int search(vector<int>& nums, int target) { int r=nums.size()-1; int l=0; while (l <= r) { int m = l + (r - l) / 2; // Check if target is present at mid if (nums[m] == target) return m; // If target greater, ignore left half if (nums[m] < target) l = m + 1; // If target is smaller, ignore right half else r = m - 1; } return -1; } };
Top comments (0)