S Sreekantha Reddy
Topic: Binary Search
1 Basic Level Binary Search Algorithm
Question 1
Given an array of integers nums which is sorted in ascending order, and an integer
target, write a function to search for target in nums. If target exists, then return
its index. Otherwise, return -1. Your implementation must have a runtime complexity
of O(log n).
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 integers in nums are unique.
nums is sorted in ascending order.
Requirements:
Write a function with the following signature:
int search(int[] nums, int target)
Ensure the algorithm runs in O(log n) time complexity.
1
Question 2
Given a sorted array of distinct integers and a target value, return the index if the
target is found. If not, return the index where it would be if it were inserted in order.
Your implementation must have a runtime complexity of O(log n).
Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
Constraints:
1 ≤ nums.length ≤ 104
−104 ≤ nums[i] ≤ 104
nums contains distinct values sorted in ascending order.
−104 ≤ target ≤ 104
2
Question 3
Given a non-negative integer x, return the square root of x rounded down to the nearest
integer. The returned integer should be non-negative as well. You must not use any
built-in exponent function or operator. For example, do not use pow(x, 0.5) in C++
or x ** 0.5 in Python.
Example 1:
Input: x = 4
Output: 2
Explanation: The square root of 4 is 2, so we return 2.
Example 2:
Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we round it down
to the nearest integer, 2 is returned.
Constraints:
0 ≤ x ≤ 231 − 1
3
Question 4
Given a positive integer num, return true if num is a perfect square or false otherwise.
A perfect square is an integer that is the square of an integer. In other words, it is the
product of some integer with itself.
You must not use any built-in library function, such as sqrt.
Example 1:
Input: num = 16
Output: true
Explanation: We return true because 4 * 4 = 16 and 4 is an integer.
Example 2:
Input: num = 14
Output: false
Explanation: We return false because 3.742 * 3.742 = 14 and 3.742 is not
an integer.
Constraints:
1 ≤ num ≤ 231 − 1
4
2 Moderate Level Binary Search Algorithm
Question 5
Given an array of integers A of size N and an integer B, array A is rotated at some pivot
unknown to you beforehand (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
You are given a target value B to search. If found in the array, return its index; otherwise,
return -1.
You may assume no duplicates exist in the array.
NOTE: Array A was sorted in non-decreasing order before rotation.
Think about the case when there are duplicates. Does your current solution work? How
does the time complexity change?
Constraints:
1 ≤ N ≤ 106
1 ≤ A[i] ≤ 109
1 ≤ B ≤ 109
All elements in A are distinct.
Question 6
Suppose a sorted array A is rotated at some pivot unknown to you beforehand.
(i.e., [1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 1, 2]).
Find the minimum element.
The array will not contain duplicates.
Note: Use the circular rotated property of the array to solve the problem.
Constraints:
1 ≤ len(A) ≤ 105
1 ≤ A[i] ≤ 109
5
Question 7
Search in Bitonic Array!
Given a bitonic sequence A of N distinct elements, write a program to find a given
element B in the bitonic sequence in O(log N ) time.
NOTE:
A Bitonic Sequence is a sequence of numbers which is first strictly increasing then after
a point strictly decreasing.
Example 1:
Input: A = [3, 9, 10, 20, 17, 5, 1], B = 20
Output: 3
Example 2:
Input: A = [5, 6, 7, 8, 9, 10, 3, 2, 1], B = 30
Output: -1
Constraints:
3 ≤ N ≤ 105
1 ≤ A[i], B ≤ 108
Given array always contains a bitonic point.
Array A always contains distinct elements.
6
Advanced
Question 8: Wood Cutting
There is an integer array A of size N denoting the heights of N trees.
Lumberjack Ojas needs to chop down B metres of wood. Ojas sets a height parameter
H (in metres), and the machine raises a giant sawblade to that height and cuts off all
tree parts higher than H. Ojas wants to set his sawblade as high as possible while still
obtaining at least B metres of wood. Help Ojas find the maximum integer height of the
sawblade that allows him to achieve this.
NOTE: The sum of all heights will exceed B, so Ojas will always be able to obtain the
required amount of wood.
Constraints:
1 ≤ N ≤ 106
1 ≤ A[i] ≤ 106
1 ≤ B ≤ 2 · 106
Question 9: Minimum Number of Days to Make m Bouquets
You are given an integer array bloomDay, an integer m, and an integer k.
You want to make m bouquets. To make a bouquet, you need k adjacent flowers from
the garden. Return the minimum number of days you need to wait to be able to make
m bouquets, or -1 if it is impossible.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Constraints:
7
1 ≤ n ≤ 105
1 ≤ bloomDay[i] ≤ 109
1 ≤ m ≤ 106
1≤k≤n
Question 10: Allocate Books
Given an array of integers A of size N and an integer B, allocate books to B students
such that the maximum number of pages allocated to a student is minimized.
Conditions:
A book will be allocated to exactly one student.
Each student has to be allocated at least one book.
Allotment should be in contiguous order, for example: a student cannot be allo-
cated book 1 and book 3, skipping book 2.
Calculate and return that minimum possible number.
NOTE: Return -1 if a valid assignment is not possible.
Example 1:
Input: A = [12, 34, 67, 90], B = 2
Output: 113
Example 2:
Input: A = [5, 17, 100, 11], B = 4
Output: 100
Constraints:
1 ≤ N ≤ 105
1 ≤ A[i], B ≤ 105
8
Question 11: Capacity to Ship Packages Within B Days
A conveyor belt has packages that must be shipped within B days. The ith package
has a weight of A[i]. Determine the least weight capacity of the ship to achieve this.
Conditions:
Each day, packages are loaded in the order given by A.
The ship cannot load more weight than its capacity.
Return the least weight capacity of the ship.
Example 1:
Input: A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], B = 5
Output: 15
Example 2:
Input: A = [3, 2, 2, 4, 1, 4], B = 3
Output: 6
Constraints:
1 ≤ B ≤ |A| ≤ 5 · 105
1 ≤ A[i] ≤ 105
Question 12: Painter’s Partition Problem (Continued)
Given A painters and an array C representing the lengths of boards, calculate the mini-
mum time required to paint all boards under the constraints:
Each painter takes B units of time per unit length.
Each painter paints contiguous boards only.
Boards cannot be shared between painters.
Return the result modulo 107 + 3.
Example 1:
Input: A = 2, B = 5, C = [1, 10]
Output: 50
9
Example 2:
Input: A = 10, B = 1, C = [1, 8, 11, 3]
Output: 11
Constraints:
1 ≤ A ≤ 1000
1 ≤ B ≤ 106
1 ≤ N ≤ 105
1 ≤ C[i] ≤ 106
Question 13: Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of
the two sorted arrays.
The overall runtime complexity should be O(log(m + n)).
NOTE: If the number of elements in the merged array is even, the median is the
average of the n/2-th and n/2 + 1-th elements.
Example 1:
Input: A = [1, 4, 5], B = [2, 3]
Output: 3
Example Explanation:
Merged array: [1, 2, 3, 4, 5]
Median: 3
Constraints:
0 ≤ |A|, |B| ≤ 106
1 ≤ |A| + |B| ≤ 2 · 106
10
Programming Exam: Matrix Search Problem
Question 14: Matrix Search
You are given an m × n integer matrix matrix with the following two properties:
Each row is sorted in non-decreasing order.
The first integer of each row is greater than the last integer of the previous row.
Given an integer target, return true if target is in matrix or false otherwise.
You must write a solution with O(log(m × n)) time complexity.
Example 1:
Input:
1 3 5 7
10 11 16 20
23 30 34 60
target = 3
Output: true
Example 2:
Input:
1 3 5 7
10 11 16 20
23 30 34 60
target = 13
Output: false
11