Binary Search

Binary Search

Forty-five questions on binary search, from searching sorted arrays to searching directly on the answer space.

Arranging Coins

easy
You have n coins and you want to build a staircase with these coins.

Binary Search

easy
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums.

Check If N and Its Double Exist

easy
Given an array arr of integers, check if there exist two indices i and j such that :

Count Negative Numbers in a Sorted Matrix

easy
Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.

Count the Number of Incremovable Subarrays I

easy
You are given a 0-indexed array of positive integers nums.

Fair Candy Swap

easy
Alice and Bob have a different total number of candies.

Find Smallest Letter Greater Than Target

easy
You are given an array of characters letters that is sorted in non-decreasing order, and a character target.

Find Target Indices After Sorting Array

easy
You are given a 0-indexed integer array nums and a target element target.

Find the Distance Value Between Two Arrays

easy
Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays.

Fruits Into Baskets II

easy
You are given two arrays of integers, fruits and baskets, each of length n, where fruits[i] represents the quantity of the ith type of fruit, and baskets[j] represents the...

Intersection of Two Arrays

easy
Given two integer arrays nums1 and nums2, return an array of their intersection.

Intersection of Two Arrays II

easy
Given two integer arrays nums1 and nums2, return an array of their intersection.

Kth Missing Positive Number

easy
Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.

Longest Subsequence With Limited Sum

easy
You are given an integer array nums of length n, and an integer array queries of length m.

Maximum Count of Positive Integer and Negative Integer

easy
Given an array nums sorted in non-decreasing order, return the maximum between the number of positive integers and the number of negative integers.

Minimum Common Value

easy
Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays.

Missing Number

easy
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

Search Insert Position

easy
Given a sorted array of distinct integers and a target value, return the index if the target is found.

Special Array With X Elements Greater Than or Equal X

easy
You are given an array nums of non-negative integers.

Sqrt(x)

easy
Given a non-negative integer x, return the square root of x rounded down to the nearest integer.

Valid Perfect Square

easy
Given a positive integer num, return true if num is a perfect square or false otherwise.

132 Pattern

medium
Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].

Adjacent Increasing Subarrays Detection II

medium
Given an array nums of n integers, your task is to find the maximum value of k for which there exist two adjacent subarrays of length k each, such that both subarrays are strictly...

Avoid Flood in The City

medium
Your country has an infinite number of lakes.

Capacity To Ship Packages Within D Days

medium
A conveyor belt has packages that must be shipped from one port to another within days days.

Closest Equal Element Queries

medium
You are given a circular array nums and an array queries.

Compare Strings by Frequency of the Smallest Character

medium
Let the function f(s) be the frequency of the lexicographically smallest character in a non-empty string s.

Count Number of Rectangles Containing Each Point

medium
You are given a 2D integer array rectangles where rectangles[i] = [li, hi] indicates that ith rectangle has a length of li and a height of hi.

Count the Number of Fair Pairs

medium
Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs.

Earliest Second to Mark Indices I

medium
You are given two 1-indexed integer arrays, nums and, changeIndices, having lengths n and m, respectively.

Find a Peak Element II

medium
A peak element in a 2D grid is an element that is strictly greater than all of its adjacent neighbors to the left, right, top, and bottom.

Find Beautiful Indices in the Given Array I

medium
You are given a 0-indexed string s, a string a, a string b, and an integer k.

Find First and Last Position of Element in Sorted Array

medium
Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

Find K Closest Elements

medium
Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array.

Find Latest Group of Size M

medium
Given an array arr that represents a permutation of numbers from 1 to n.

Find Longest Special Substring That Occurs Thrice I

medium
You are given a string s that consists of lowercase English letters.

Find Longest Special Substring That Occurs Thrice II

medium
You are given a string s that consists of lowercase English letters.

Find Minimum in Rotated Sorted Array

medium
Suppose an array of length n sorted in ascending order is rotated between 1 and n times.

Find Peak Element

medium
A peak element is an element that is strictly greater than its neighbors.

Find the Duplicate Number

medium
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.

Find the Longest Equal Subarray

medium
You are given a 0-indexed integer array nums and an integer k.

Find the Maximum Number of Marked Indices

medium
You are given a 0-indexed integer array nums.

Find the Safest Path in a Grid

medium
You are given a 0-indexed 2D matrix grid of size n x n, where (r, c) represents: You are initially positioned at cell (0, 0).

Find the Smallest Divisor Given a Threshold

medium
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor, divide all the array by it, and sum the division's result.

Find the Student that Will Replace the Chalk

medium
There are n students in a class numbered from 0 to n - 1.