Problem Statement
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
Approach
Initialize two pointers, left and right. While left <= right, calculate the middle index. If nums[mid] equals the target, return mid. If nums[mid] < target, search the right half (left = mid + 1). If nums[mid] > target, search the left half (right = mid - 1).
Time & Space Complexity
Time complexity is O(log n) because the search space is halved in each step. Space complexity is O(1) for iterative implementation.
