0% found this document useful (0 votes)
39 views11 pages

Binary Search

The document outlines various binary search problems, including searching for a target in a sorted array, finding the square root of a number, and determining if a number is a perfect square. It also covers moderate to advanced level problems such as searching in rotated arrays, bitonic sequences, and optimizing resource allocation for tasks like wood cutting and bouquet making. Each problem includes constraints, examples, and requirements for time complexity, primarily aiming for O(log n) efficiency.

Uploaded by

nabinkoirala53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views11 pages

Binary Search

The document outlines various binary search problems, including searching for a target in a sorted array, finding the square root of a number, and determining if a number is a perfect square. It also covers moderate to advanced level problems such as searching in rotated arrays, bitonic sequences, and optimizing resource allocation for tasks like wood cutting and bouquet making. Each problem includes constraints, examples, and requirements for time complexity, primarily aiming for O(log n) efficiency.

Uploaded by

nabinkoirala53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like