Practice/Google/Leetcode 704. Binary Search
CodingMust
You are given an array of integers sorted in ascending order, where all elements are distinct. Your task is to find the position (index) of a specific target value within this array.
If the target value exists in the array, return its index. If the target value is not present, return -1.
Your algorithm must achieve O(log n) runtime complexity, where n is the length of the array.
Example 1:
Input: nums = [-1, 0, 3, 5, 9, 12], target = 9 Output: 4 Explanation: The value 9 appears at index 4 in the array
Example 2:
Input: nums = [-1, 0, 3, 5, 9, 12], target = 2 Output: -1 Explanation: The value 2 does not exist in the array
Example 3:
Input: nums = [5], target = 5 Output: 0 Explanation: Single element array where the target matches