Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»Data Structures & Algorithms»Binary Search | Leetcode 704 | Explained in Python
    Data Structures & Algorithms

    Binary Search | Leetcode 704 | Explained in Python

    codeanddebugBy codeanddebug2 July 2025No Comments5 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Featured image of question to implement binary search
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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.

    Here’s the [Problem Link] to begin with.

    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 <= 104
    • -104 < nums[i], target < 104
    • All the integers in nums are unique.
    • nums is sorted in ascending order.
    Contents:
    • OPTIMAL SOLUTION
      • 1. Intuition & Approach (Breaking it Down Step-by-Step)
        • Understanding the Problem
        • Why Binary Search?
        • How Binary Search Works
      • 2. Code Implementation
      • 3. Code Explanation (High-Level Overview)
        • Step 1: Initialize Search Range
        • Step 2: Loop Until low Exceeds high
        • Step 3: Compute the Middle Index
        • Step 4: Compare nums[mid] with target
        • Step 5: Return -1 if Target is Not Found
      • 4. Dry Run (Step-by-Step Execution)
        • Example 1
        • Execution Process
        • Final Output:
        • Example 2 (Target Not Found)
        • Final Output:
      • 5. Time and Space Complexity Analysis
        • Time Complexity:
        • Space Complexity:
      • 6. Edge Cases to Consider
        • 1. Target is the First or Last Element
        • 2. Target is Not in the Array
        • 3. Empty Array
        • 4. Array Contains One Element
        • 5. Large Input Case
      • Conclusion

    OPTIMAL SOLUTION

    1. Intuition & Approach (Breaking it Down Step-by-Step)

    Understanding the Problem

    We are given a sorted array (nums) and a target integer (target). Our goal is to determine the index of target in nums. If target is not found, we return -1.

    Why Binary Search?

    Instead of checking each element one by one (O(N) time complexity) like linear search, we can leverage the fact that the array is sorted and efficiently reduce our search space by half in each step.

    How Binary Search Works

    1. Set two pointers (low and high) at the beginning (0) and end (n-1) of the array.
    2. Find the middle index (mid) of the current search range.
    3. Compare nums[mid] with target:
      • If nums[mid] == target, we return mid (found the target).
      • If nums[mid] < target, it means target must be on the right half, so we move low = mid + 1.
      • If nums[mid] > target, it means target must be on the left half, so we move high = mid – 1.
    4. Repeat the process until low > high (which means target is not present in the array).

    This approach eliminates half of the remaining elements at each step, making it extremely fast compared to scanning the array linearly.

    2. Code Implementation

    class Solution:
        def search(self, nums: List[int], target: int) -> int:
            n = len(nums)
            low = 0
            high = n - 1
    
            while low <= high:
                mid = (low + high) // 2  # Find the middle index
    
                if nums[mid] == target:
                    return mid  # Target found, return index
    
                elif nums[mid] < target:
                    low = mid + 1  # Move to the right half
    
                else:
                    high = mid - 1  # Move to the left half
    
            return -1  # Target not found
    Python

    3. Code Explanation (High-Level Overview)

    Step 1: Initialize Search Range

    • We define low = 0 (start) and high = n – 1 (end) to keep track of our search space.

    Step 2: Loop Until low Exceeds high

    • The loop runs until low <= high, ensuring we continue searching as long as there is a valid range.

    Step 3: Compute the Middle Index

    • The middle index is calculated using: mid = (low+high) / 2
    • This splits the array in half at each step.

    Step 4: Compare nums[mid] with target

    1. If nums[mid] == target, we return mid (target found).
    2. If nums[mid] < target, move to the right half (low = mid + 1).
    3. If nums[mid] > target, move to the left half (high = mid – 1).

    Step 5: Return -1 if Target is Not Found

    • If the loop exits without finding the target, we return -1 (indicating absence in the array).

    4. Dry Run (Step-by-Step Execution)

    Example 1

    nums = [-1, 0, 3, 5, 9, 12]
    target = 9

    Execution Process

    Steplowhighmidnums[mid]Action
    105233 < 9, move right (low = 3)
    23549✅ Target found, return 4

    Final Output:

    4

    Example 2 (Target Not Found)

    nums = [-1, 0, 3, 5, 9, 12]
    target = 2
    Steplowhighmidnums[mid]Action
    105233 > 2, move left (high = 1)
    2010-1-1 < 2, move right (low = 1)
    311100 < 2, move right (low = 2)
    • Since low (2) > high (1), loop terminates and returns -1 (target not found).

    Final Output:

    -1

    5. Time and Space Complexity Analysis

    Time Complexity:

    • At each step, we divide the search space in half.
    • The maximum number of steps required to find an element in an array of size N is log₂(N).
    • Time complexity: O(log⁡N)
    • This is significantly faster than a linear search (O(N)).

    Space Complexity:

    • The algorithm only uses a few integer variables (low, high, mid).
    • No extra memory is used, so space complexity is: O(1)

    6. Edge Cases to Consider

    1. Target is the First or Last Element

    nums = [1, 2, 3, 4, 5]
    target = 1  # First element

    Expected Output: 0

    nums = [1, 2, 3, 4, 5]
    target = 5  # Last element

    Expected Output: 4

    2. Target is Not in the Array

    nums = [2, 4, 6, 8, 10]
    target = 5

    Expected Output: -1 (not found)

    3. Empty Array

    nums = []
    target = 3

    Expected Output: -1 (no elements to search)

    4. Array Contains One Element

    nums = [7]
    target = 7

    Expected Output: 0 (found at index 0)

    5. Large Input Case

    nums = list(range(1, 1000001))  # 1 to 1,000,000
    target = 999999

    Binary search finds it in log₂(10⁶) ≈ 20 comparisons instead of 1,000,000 comparisons in linear search.

    Conclusion

    ✅ Why This Solution is Optimal?

    • O(log N) complexity makes it highly efficient for large datasets.
    • O(1) space complexity (no extra storage required).
    • Works for any sorted array, regardless of size.
    • Much faster than linear search, especially for large inputs.
    Join our Advance DSA COURSE

    For any changes to the article, kindly email at code@codeanddebug.in or contact us at +91-9712928220.

    Array Binary Search
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleMajority Element II | Leetcode 229 | Explained in Python
    Next Article Implement Lower Bound | Floor in a Sorted Array (GFG)
    codeanddebug
    • Website

    Related Posts

    Data Structures & Algorithms

    Search in Rotated Sorted Array | Leetcode 33 | Optimal Binary Search

    3 July 2025
    Data Structures & Algorithms

    Count occurrences of a number in a sorted array with duplicates | Optimal Binary Search

    3 July 2025
    Data Structures & Algorithms

    Find First and Last Position of Element in Sorted Array | Leetcode 34 | Optimal Binary Search

    2 July 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • Data Structures & Algorithms (93)
      • Beginner (44)
      • Expert (22)
      • Intermediate (27)
    • Uncategorised (1)
    Recent Posts

    Search in Rotated Sorted Array | Leetcode 33 | Optimal Binary Search

    3 July 2025

    Count occurrences of a number in a sorted array with duplicates | Optimal Binary Search

    3 July 2025

    Find First and Last Position of Element in Sorted Array | Leetcode 34 | Optimal Binary Search

    2 July 2025

    Ceil The Floor | Binary Search Implementation

    2 July 2025

    Search Insert Position | Leetcode 35 | Explained in Python

    2 July 2025
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.