# Solution For Binary Search

As an AI language model, I do not have access to the specific problem you are referring to on LeetCode. However, I can provide a general explanation of the binary search algorithm and how it can be applied to solve problems.

Binary search is a searching algorithm that searches for a specific value in an ordered list, array or sequence by dividing the search range in half repeatedly until the value is found or the search range is empty. It is based on the divide and conquer strategy and is very efficient for large data sets.

The steps involved in implementing binary search are:

1. Determine the middle element of the sequence.

2. Compare the middle element with the target value.

3. If the middle element is equal to the target value, then the search is successful and its index is returned.

4. If the middle element is greater than the target value, then search the left sub-sequence.

5. If the middle element is less than the target value, then search the right sub-sequence.

6. Repeat steps 1-5 until either the target value is found or the sub-sequence is empty.

Binary search has a time complexity of O(log n), which is faster than linear search with a time complexity of O(n).

To solve a binary search problem on LeetCode, you will need to analyze the problem requirements, constraints, and examples. You will need to implement the binary search algorithm and customize it to the specific problem requirements. This may involve modifying the search range, handling edge cases, or keeping track of additional variables during the search.

In general, the binary search algorithm is a powerful tool for solving problems that involve searching for a specific value in a large data set. With a thorough understanding of the algorithm and its applications, you can solve many challenging problems on LeetCode and other coding platforms.

## Step by Step Implementation For Binary Search

```class Solution {
public int search(int[] nums, int target) {

if (nums == null || nums.length == 0) {
return -1;
}

int left = 0;
int right = nums.length - 1;

while (left <= right) {
int mid = left + (right - left) / 2;

if (nums[mid] == target) {
return mid;
}
else if (nums[mid] < target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}

return -1;
}
}```
```class Solution:
def search(self, nums: List[int], target: int) -> int:
if not nums:
return -1

left = 0
right = len(nums) - 1

while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1

return -1```
```Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.

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

Note:

You may assume that all elements in nums are unique.
n will be in the range [1, 10000].
The value of each element in nums will be in the range [-9999, 9999].

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
// set left and right pointers
let left = 0;
let right = nums.length - 1;

// while left pointer is less than right pointer
while(left <= right) {
// set middle pointer to the average of left and right
let middle = Math.floor((left + right) / 2);

// if target is equal to the value at the middle pointer, return middle
if(target === nums[middle]) {
return middle;
}
// if target is less than the value at the middle pointer, move right pointer to the left of the middle
else if(target < nums[middle]) {
right = middle - 1;
}
// if target is greater than the value at the middle pointer, move left pointer to the right of the middle
else {
left = middle + 1;
}
}

return -1;
};```
```class Solution {
public:
int search(vector& nums, int target) {
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
};```
`public int BinarySearch(int[] nums, int target) { // check for empty or null array if (nums == null || nums.Length == 0) { return -1; } int left = 0; int right = nums.Length - 1; while (left <= right) { // calculate the midpoint int mid = left + (right - left) / 2; // check if the target is at the midpoint if (nums[mid] == target) { return mid; } // if the target is less than the midpoint, search the left side of the array else if (target < nums[mid]) { right = mid - 1; } // if the target is greater than the midpoint, search the right side of the array else { left = mid + 1; } } // target was not found return -1; }`

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]