Binary Search
Binary Search
Forty-five questions on binary search, from searching sorted arrays to searching directly on the answer space.
Arranging Coins
easyYou have n coins and you want to build a staircase with these coins.
Binary Search
easyGiven 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
easyGiven an array arr of integers, check if there exist two indices i and j such that :
Count Negative Numbers in a Sorted Matrix
easyGiven 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
easyYou are given a 0-indexed array of positive integers nums.
Fair Candy Swap
easyAlice and Bob have a different total number of candies.
Find Smallest Letter Greater Than Target
easyYou are given an array of characters letters that is sorted in non-decreasing order, and a character target.
Find Target Indices After Sorting Array
easyYou are given a 0-indexed integer array nums and a target element target.
Find the Distance Value Between Two Arrays
easyGiven two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays.
Fruits Into Baskets II
easyYou 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
easyGiven two integer arrays nums1 and nums2, return an array of their intersection.
Intersection of Two Arrays II
easyGiven two integer arrays nums1 and nums2, return an array of their intersection.
Kth Missing Positive Number
easyGiven an array arr of positive integers sorted in a strictly increasing order, and an integer k.
Longest Subsequence With Limited Sum
easyYou 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
easyGiven 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
easyGiven two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays.
Missing Number
easyGiven 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
easyGiven 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
easyYou are given an array nums of non-negative integers.
Sqrt(x)
easyGiven a non-negative integer x, return the square root of x rounded down to the nearest integer.
Valid Perfect Square
easyGiven a positive integer num, return true if num is a perfect square or false otherwise.
132 Pattern
mediumGiven 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
mediumGiven 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
mediumYour country has an infinite number of lakes.
Capacity To Ship Packages Within D Days
mediumA conveyor belt has packages that must be shipped from one port to another within days days.
Closest Equal Element Queries
mediumYou are given a circular array nums and an array queries.
Compare Strings by Frequency of the Smallest Character
mediumLet 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
mediumYou 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
mediumGiven 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
mediumYou are given two 1-indexed integer arrays, nums and, changeIndices, having lengths n and m, respectively.
Find a Peak Element II
mediumA 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
mediumYou 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
mediumGiven 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
mediumGiven 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
mediumGiven an array arr that represents a permutation of numbers from 1 to n.
Find Longest Special Substring That Occurs Thrice I
mediumYou are given a string s that consists of lowercase English letters.
Find Longest Special Substring That Occurs Thrice II
mediumYou are given a string s that consists of lowercase English letters.
Find Minimum in Rotated Sorted Array
mediumSuppose an array of length n sorted in ascending order is rotated between 1 and n times.
Find Peak Element
mediumA peak element is an element that is strictly greater than its neighbors.
Find the Duplicate Number
mediumGiven an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
Find the Longest Equal Subarray
mediumYou are given a 0-indexed integer array nums and an integer k.
Find the Maximum Number of Marked Indices
mediumYou are given a 0-indexed integer array nums.
Find the Safest Path in a Grid
mediumYou 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
mediumGiven 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
mediumThere are n students in a class numbered from 0 to n - 1.