101 Logo
onenoughtone

Solution Approach

Solution Overview

There are 2 main approaches to solve this problem:

  1. Recursive Binary Search - Complex approach
  2. Iterative Binary Search - Complex approach

Approach 1: Recursive Binary Search

The recursive binary search approach is a classic example of the Divide & Conquer pattern. We repeatedly divide the search space in half by comparing the target value with the middle element of the current subarray.

This approach is particularly efficient for large datasets because it eliminates half of the remaining elements in each step, resulting in a logarithmic time complexity.

Algorithm:

  1. Define a recursive function binarySearch(bookCodes, target, left, right) that takes the array of book codes, the target code, and the left and right boundaries of the current search space.
  2. Base case: If left > right, the target is not in the array, so return -1.
  3. Calculate the middle index as mid = Math.floor((left + right) / 2).
  4. If bookCodes[mid] equals the target, return mid (we found the book).
  5. If bookCodes[mid] > target, recursively search in the left half by calling binarySearch(bookCodes, target, left, mid - 1).
  6. If bookCodes[mid] < target, recursively search in the right half by calling binarySearch(bookCodes, target, mid + 1, right).
  7. The main function should initialize left = 0 and right = bookCodes.length - 1, then call the recursive function.

Time Complexity:

O(log n)

In each recursive call, we eliminate half of the remaining elements, resulting in a logarithmic time complexity.

Space Complexity:

O(log n)

The recursion stack will have at most log n frames, corresponding to the maximum depth of the recursion.

Approach 2: Iterative Binary Search

While the problem asks for a recursive solution, it's worth mentioning the iterative approach as well. This approach uses a loop instead of recursion to implement binary search.

The iterative approach has the same time complexity as the recursive approach but uses constant space, making it more efficient in terms of space complexity.

Algorithm:

  1. Initialize left = 0 and right = bookCodes.length - 1.
  2. While left <= right:
  3. - Calculate the middle index as mid = Math.floor((left + right) / 2).
  4. - If bookCodes[mid] equals the target, return mid (we found the book).
  5. - If bookCodes[mid] > target, update right = mid - 1 (search in the left half).
  6. - If bookCodes[mid] < target, update left = mid + 1 (search in the right half).
  7. If the loop ends without finding the target, return -1.

Time Complexity:

O(log n)

Like the recursive approach, we eliminate half of the remaining elements in each iteration.

Space Complexity:

O(1)

The iterative approach uses a constant amount of extra space, regardless of the input size.

Pseudocode

solution.pseudo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function binarySearch(bookCodes, targetCode):
return binarySearchRecursive(bookCodes, targetCode, 0, bookCodes.length - 1)
function binarySearchRecursive(bookCodes, targetCode, left, right):
// Base case: target not found
if left > right:
return -1
// Calculate middle index
mid = floor((left + right) / 2)
// Check if we found the target
if bookCodes[mid] == targetCode:
return mid
// If target is smaller, search in the left half
if bookCodes[mid] > targetCode:
return binarySearchRecursive(bookCodes, targetCode, left, mid - 1)
// If target is larger, search in the right half
return binarySearchRecursive(bookCodes, targetCode, mid + 1, right)
ProblemSolutionCode
101 Logo
onenoughtone