Binary Search Target Index
Given a sorted array of integers in ascending order and a target value, write a function to find the index of the target value in the array using binary search. If the target is not present in the array, return -1.
Problem Details: The input array is guaranteed to be sorted in ascending order with no duplicate elements. Your task is to implement a binary search algorithm to locate the target value efficiently. Binary search works by repeatedly dividing the search interval in half. If the target value is found, return its index; otherwise, return -1. The solution should handle edge cases, such as an empty array or a target value not present in the array.
Algorithm Flow

Recommendation Algorithm Flow for Binary Search Target Index - Budibadu
Best Answers
java
class Solution {
public int search(Object nums, Object target) {
int[] arr = (int[]) nums;
int t = (int) target;
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == t) {
return mid;
} else if (arr[mid] < t) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
