Binary Search - aloalgo

Binary Search

Easy

You are given a sorted array of integers called nums and an integer target.

Your task is to search for target within nums.

You may assume that:

  • nums is sorted in ascending order.

Return the index of target if found, or -1 otherwise.

Example 1

Inputs
nums = [1, 3, 5, 7, 9]
target = 5
Output
2
Explanation:

The target 5 is at index 2 in the array.

Example 2

Inputs
nums = [2, 4, 6, 8, 10]
target = 7
Output
-1
Explanation:

The target 7 does not exist in the array.

Example 3

Inputs
nums = [0, 1, 2, 3, 4, 5]
target = 0
Output
0
Explanation:

The target 0 is at the very first index.

Loading...
Inputs
nums = [1, 3, 5, 7, 9]
target = 5
Output
2

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!