Binary Search Practice

Challenge Difficulty: Medium | Estimated completion time: ~20 minutes

Given a sorted array of integers and a target integer, find the first occurrence of the target and return its index.

Return -1 if the target is not in the array.

Examples

#Input:
arr = [1, 3, 3, 3, 3, 6, 10, 10, 10, 100]
target = 3
 
find_first_occurrence(arr,target) # Return 1
#Explanation: The first occurrence of 3 is at index 1.
 
#Input:
arr = [2, 3, 5, 7, 11, 13, 17, 19]
target = 6
 
find_first_occurrence(arr,target) # Return -1
#Explanation: 6 does not exist in the array.

Solution

Python
Output