Binary Search

Binary Search

Advanced binary search techniques and applications

18 Problems
Intermediate
Watch Playlist

All Problems

18 problems
1
Binary Search in a Sorted Array
Given a sorted array of integers `nums` and an integer `target`, write a function to search for the `target` in `nums`. If the target exists, then return its index. Otherwise, return -1.
Easy
Not Attempted
Video
2
Binary Search on a Reverse Sorted Array
Given a reverse sorted array of integers `nums` (sorted in descending order), and an integer `target`, write a function to search for the `target` in `nums`. If the target exists, return its index. Otherwise, return -1.
Easy
Not Attempted
Video
3
Find First and Last Occurrence of an Element
Given a sorted array `nums` with possible duplicate elements, find the first and last occurrences of a given `target` value. If the target is not found in the array, return `[-1, -1]`.
Medium
Not Attempted
Video
4
Count of an Element in a Sorted Array
Given a sorted array `nums` and a `target` value, return the number of times the target appears in the array. If the target is not found, return 0.
Medium
Not Attempted
Video
5
Number of Times a Sorted Array is Rotated
Given a sorted array of distinct integers which is rotated at some unknown pivot, find the number of times the array has been rotated. This is equivalent to finding the index of the minimum element in the array.
Medium
Not Attempted
Video
6
Find an Element in a Rotated Sorted Array
Given a sorted array of distinct integers that has been rotated at an unknown pivot, and a target value, find the index of the target if it exists in the array. Otherwise, return -1.
Medium
Not Attempted
Video
7
Search in a Nearly Sorted Array
Given an array where each element is at most `k` positions away from its sorted position (often k=1), find the index of a given target element. If the element is not present, return -1.
Medium
Not Attempted
Video
8
Find Floor of an Element in a Sorted Array
Given a sorted array `nums` and a value `x`, find the floor of `x` in the array. The floor is the greatest element in the array that is less than or equal to `x`. If no such element exists, return -1.
Easy
Not Attempted
Video
9
Find Ceil of an Element in a Sorted Array
Given a sorted array `nums` and a value `x`, find the ceiling of `x` in the array. The ceiling is the smallest element in the array that is greater than or equal to `x`. If no such element exists, return -1.
Easy
Not Attempted
Video
10
Next Alphabetical Element
Given a sorted array of characters `letters` and a `target` character, find the smallest character in the array that is strictly greater than the `target`. If no such character exists, handle accordingly (e.g., wrap around or return a default value).
Easy
Not Attempted
Video
11
Position of an Element in an Infinite Sorted Array
Given a sorted array of unknown size (infinite) and a target element, find the position of the target. You cannot use `length` property of the array.
Medium
Not Attempted
Video
12
Index of First 1 in a Binary Sorted Infinite Array
Given an infinite sorted binary array (containing only 0s and 1s), find the index of the first occurrence of 1.
Medium
Not Attempted
Video
13
Minimum Difference Element in a Sorted Array
Given a sorted array and a target key, find the element in the array that has the minimum absolute difference with the given key.
Medium
Not Attempted
Video
14
Find Peak Element
A peak element is an element that is strictly greater than its neighbors. Given an integer array `nums`, find a peak element and return its index. The array may contain multiple peaks; in that case, return the index to any of the peaks. You may imagine that `nums[-1] = nums[n] = -∞`.
Medium
Not Attempted
Video
15
Find Maximum Element in a Bitonic Array
A bitonic array is an array that first increases and then possibly decreases. Find the maximum value in such an array. The maximum element is the peak of the array.
Medium
Not Attempted
Video
16
Search in a Bitonic Array
Given a bitonic array and a target key, find if the key is present in the array. Return its index if found, otherwise return -1.
Hard
Not Attempted
Video
17
Search in a Row-wise and Column-wise Sorted Matrix
Given an `m x n` matrix where each row is sorted in ascending order and each column is also sorted in ascending order, write an efficient algorithm to search for a target value.
Medium
Not Attempted
Video
18
Allocate Minimum Number of Pages
Given an array of integers `pages` representing the number of pages in `n` books and a number of students `k`, allocate the books to `k` students such that each student gets at least one book, all books are assigned, and each assignment is a contiguous block of books. The goal is to minimize the maximum number of pages assigned to any single student.
Hard
Not Attempted
Video