##### Problem Statement

Link: LeetCode

Complexity: **Easy**

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`

.

You must write an algorithm with `O(log n)`

runtime complexity.

**Example 1:**

Input:nums = [-1,0,3,5,9,12], target = 9Output:4Explanation:9 exists in nums and its index is 4

**Example 2:**

Input:nums = [-1,0,3,5,9,12], target = 2Output:-1Explanation:2 does not exist in nums so return -1

**Constraints:**

`1 <= nums.length <= 10`

^{4}`-10`

^{4}< nums[i], target < 10^{4}- All the integers in
`nums`

are**unique**. `nums`

is sorted in ascending order.

##### Solution Explanation:

###### References:

To achieve a time complexity of O(log n), we can use binary search algorithm.

The idea of binary search is to divide the search space in half at each step. We start by setting two pointers, ‘

‘ and *left*

‘, to the beginning and end of the array, respectively. We then calculate the midpoint of the search space as *'right*`mid = `

* '(left + right) // 2‘*.

We compare the value at the midpoint `nums'`

with the target value. If they are equal, we have found the target and return its index *[mid]*'`mid`

. If ‘

‘, we know that the target must be in the right half of the search space, so we move the *nums[mid] < target*`left`

pointer to ‘

‘. Otherwise, we move the *mid + 1*`right`

pointer to* *‘* mid - 1*‘.

We repeat this process until we find the target or the search space is empty, which is when ‘

‘. If we don’t find the target by then, we return *left > right*`-1`

.

*The time complexity of this algorithm is O(log n) because we divide the search space in half at each step, which reduces the search space exponentially.*

Letâ€™s understand it with an example:

We have a sorted array: [1, 3, 4, 6, 8, 9, 11], and we need to find 4 in it.

We’d initialise the pointers to extreme ends of array, i.e., left pointer to 0 and right pointer to 1.

Then, we’ll find the middle element, the middle element is

`6`

.mid = (left + right) // 2 = (0 + 6) // 2 = 3Since the middle element is greater than the target, the target must be in the left half of the search range.

We’ll repeat the process until we find the solution or dataset is empty.

##### Solution Code:

###### Solution 1

By using ‘*enumerate*‘. It is a built-in Python function that returns an iterator of tuples containing indices and values from an iterable.

```
class Solution:
def search(self, nums: List[int], target: int) -> int:
for indx, num in enumerate(nums):
if num == target:
return indx
return -1
```

###### Solution 2

Using binary search, where we divide the sorted array in half repeatedly until the target element is found, or the search space is empty

```
class Solution:
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = (right + left) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
```