Given a sorted array of integers and a target value, return the index of the target element if it is present in the array. If the target is not found, return -1. The array is sorted in non-decreasing order, and the search should be performed efficiently using binary search.
Input: arr = [1,3,5,7,9], target = 7
Output: 3
Explanation: The target value 7 is present at index 3 in the array.
Input: arr = [2,4,6,8,10], target = 5
Output: -1
Explanation: The target value 5 is not present in the array, so return -1.
Input: arr = [11,22,33,44], target = 11
Output: 0
Explanation: The target value 11 is present at index 0 in the array.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 3 5 7 9 11 13 7
3
2 4 6 8 10 12 14 12
5