Binary Search - Leetcode 704

I'm a developer who loves to write. I started my blog because I wanted to share my knowledge and passion for technology with others. I write about a variety of topics, including coding, web development, and blockchain.
Problem - Leetcode
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 = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Constraints:
1 <= nums.length <= 10<sup>4</sup>-10<sup>4</sup> < nums[i], target < 10<sup>4</sup>All the integers in
numsare unique.numsis sorted in ascending order.30 <= temperatures[i] <= 100
Solution in Golang
func search(nums []int, target int) int {
left := 0
right := len(nums)-1
for left<=right{
mid := (left+right)/2
if nums[mid]==target{
return mid
}else if nums[mid]<target{
left = mid +1
}else {
right = mid -1
}
}
return -1
}
This is a Go programming language function called search that performs a binary search to find a target integer within a sorted array of integers. Let me break down the code step by step:
func search(nums []int, target int) int {: This line defines a function namedsearchthat takes two arguments:numsis a slice of integers, representing the sorted array in which we want to search for thetargetvalue.targetis the integer we want to find in thenumsarray.The function returns an integer, which is the index of the
targetin the array if found, or -1 if it's not found.
left := 0andright := len(nums)-1: These lines initialize two variablesleftandright.leftrepresents the left boundary of the current search range, initialized to the beginning of the array (index 0), andrightrepresents the right boundary, initialized to the end of the array (indexlen(nums)-1).for left <= right {: This starts a loop that continues as long as theleftboundary is less than or equal to therightboundary. This loop is the heart of the binary search algorithm.mid := (left+right)/2: Inside the loop, it calculates the middle indexmidof the current search range by taking the average ofleftandright. This is where the binary search gets its name because it repeatedly divides the search range in half.if nums[mid] == target {: This checks if the value at the middle indexmidof the arraynumsis equal to thetarget. If it is, this means we've found thetarget, and the function returnsmid, which is the index wheretargetis located in the array.else if nums[mid] < target {: If the value atmidis less than thetarget, which means thetargetmust be in the right half of the current search range. So, it updateslefttomid + 1, effectively narrowing the search range to the right half.else {: If the value atmidis greater than thetarget, which means thetargetmust be in the left half of the current search range. So, it updatesrighttomid - 1, narrowing the search range to the left half.If the loop exits without finding the
target, i.e.,leftbecomes greater thanright, the function returns-1to indicate that thetargetis not in the array.
In summary, this code performs an efficient binary search on a sorted array to find the index of a specific target value, and it returns that index or -1 if the target is not in the array. Binary search is efficient because it repeatedly divides the search range in half, reducing the number of comparisons needed to find the target in a sorted array.




