K41
Binary Search (Standard)
Problem Statement & Objective
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums in O(log n) time.
Function Signature (Python)
binary_search(nums: list[int], target: int) -> intInput & Output Contract
Output Rule:Hedefin indeksi veya -1.
System Constraints
1 <= sayilar.length <= 10^5
-10^4 <= sayilar[i], hedef <= 10^4
Tüm sayılar benzersiz ve sıralıdırExample Execution Walkthrough
sayilar = [-1, 0, 3, 5, 9, 12], hedef = 9 -> indeks 4
sayilar = [-1, 0, 3, 5, 9, 12], hedef = 2 -> yok -> -1Example Test Scenarios (3)
Example 1
Input:
[[-1,0,3,5,9,12],9]Expected Output:
4Example 2: Bulunamadı
Input:
[[-1,0,3,5,9,12],2]Expected Output:
-1Example 3: Single Element
Input:
[[5],5]Expected Output:
0