K41

Binary Search (Standard)

Junior Level Candidate
Target Time: 15 - 20 Minutes
Type: NUMERIC

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) -> int

Input & 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ır

Example Execution Walkthrough

sayilar = [-1, 0, 3, 5, 9, 12], hedef = 9 -> indeks 4
sayilar = [-1, 0, 3, 5, 9, 12], hedef = 2 -> yok -> -1

Example Test Scenarios (3)

Example 1
Input:[[-1,0,3,5,9,12],9]
Expected Output:4
Example 2: Bulunamadı
Input:[[-1,0,3,5,9,12],2]
Expected Output:-1
Example 3: Single Element
Input:[[5],5]
Expected Output:0
Loading Python Editor...
Input Parameters
[
  [
    -1,
    0,
    3,
    5,
    9,
    12
  ],
  9
]
Expected Output
4