Binary Search Special Array with X Elements Greater Than or Equal X Solution In C++/Java/Python/JS Problem Description: You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x. Notice that x does not have to be an element in nums.
Find First and Last Position of Element in Sorted Array Solution | Code in C++/Java/Python/JS Problem Description: Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity. Examples: Input: nums = [5,7,
H-Index II Solution In C++/Java/Python/JS Problem Description: Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper and citations is sorted in ascending order, return the researcher's h-index. According to the definition of h-index on Wikipedia: The h-index is defined as the
Guess Number Higher or Lower Solution In C++/Java/Python/JS Problem Description: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
Find a Peak Element II Solution In C++/Java/Python/JS Problem Description: 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. Given a 0-indexed m x n matrix mat where no two adjacent cells are equal, find any peak element mat[i]
Minimize the Maximum of Two Arrays Solution in C++/Java/Python/JS Minimize the Maximum of Two Arrays Problem Description: We have two arrays arr1 and arr2 which are initially empty. You need to add positive integers to them such that they satisfy all the following conditions: arr1 contains uniqueCnt1 distinct positive integers, each of which is not divisible by divisor1. arr2
Binary Search Ugly Number III Solution In C++/Python/Java/JS Ugly Number III Problem Description: An ugly number is a positive integer that is divisible by a, b, or c. Given four integers n, a, b, and c, return the nth ugly number. Examples: Input: n = 3, a = 2, b = 3, c = 5 Output: 4 Explanation: The ugly numbers are
Sell Diminishing-Valued Colored Balls Solution in C++/Java/Python/JS Sell Diminishing-Valued Colored Balls Problem Description: You have an inventory of different colored balls, and there is a customer that wants to order balls of any color. The customer weirdly values the colored balls. Each colored ball's value is the number of balls of that color you currently
Minimum Absolute Difference Between Elements with Constraint Solution in C++/Java/Python/JS Problem Description: You are given a 0-indexed integer array nums and an integer x. Find the minimum absolute difference between two elements in the array that are at least x indices apart. In other words, find two indices i and j such that abs(i - j) >= x and
Binary Search Maximum White Tiles Covered by a Carpet Solution in C++/Java/Python/JS Maximum White Tiles Covered by a Carpet Problem Description: You are given a 2D integer array tiles where tiles[i] = [li, ri] represents that every tile j in the range li <= j <= ri is colored white. You are also given an integer carpetLen, the length of a single
Earliest Second to Mark Indices I Solution in C++/Java/Python/JS Earliest Second-to-Mark Indices I Problem Description: You are given two 1-indexed integer arrays, nums and, changeIndices, having lengths n and m, respectively. Initially, all indices in nums are unmarked. Your task is to mark all indices in nums. In each second, s, in order from 1 to m (inclusive), you
Heaters Leetcode Solution in C++/Java/Python/JS Heaters Problem Description: Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses. Every house can be warmed, as long as the house is within the heater's warm radius range. Given the positions
Binary Search Maximum Number of Removable Characters Solution in C++/Java/Python/JS Maximum Number of Removable Characters Problem Description: You are given two strings s and p where p is a subsequence of s. You are also given a distinct 0-indexed integer array removable containing a subset of indices of s (s is also 0-indexed). You want to choose an integer k
Minimum Time to Repair Cars Solution in C++/Java/Python/JS Minimum Time to Repair Cars Problem Description: You are given an integer array ranks representing the ranks of some mechanics. ranks[i] is the rank of the i-th mechanic. A mechanic with a rank r can repair n cars in r * n² minutes. You are also given an integer cars
Binary Search Minimum Speed to Arrive on Time Solution in C++/Java/Python/JS Minimum Speed to Arrive on Time Problem Description: You are given a floating-point number hour, representing the amount of time you have to reach the office. To commute to the office, you must take n trains in sequential order. You are also given an integer array dist of length n,
Binary Search Maximum Side Length of a Square with Sum Less than or Equal to Threshold Maximum Side Length of a Square with Sum Less than or Equal to Threshold Problem Description: Given an m * n matrix mat and an integer threshold, return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such
Aggressive Cows Leetcode Solution Aggressive Cows Problem Description: You are given an array with unique elements of stalls[], which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. Your task is to assign stalls to k cows such that the minimum distance between
Maximum Candies Allocated to K Children Solution Maximum Candies Allocated to K Children Problem Description: You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub-piles, but you cannot merge two piles together. You are also
Most Profit Assigning Work Leetcode Solution Most Profit Assigning Work Problem Description: You have n jobs and m workers. You are given three arrays: difficulty, profit, and worker where: difficulty[i] and profit[i] are the difficulty and the profit of the ith job, and worker[j] is the ability of the jth worker (i.e.
Binary Search Maximum Value at a Given Index in a Bounded Array Solution in C++/Java/Python/JS Maximum Value at a Given Index in a Bounded Array Problem Description: You are given three positive integers: n, index, and maxSum. You want to construct an array nums (0-indexed) that satisfies the following conditions: nums.length == n nums[i] is a positive integer where 0 <= i < n.
Binary Search Search in Rotated Sorted Array Leetcode Solution | Code In C++/Python/Java/JS Problem Description: There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[
Binary Search Find Peak Index in a Mountain Array Solution | Code In C++/Java/Python/JS Problem Description: You are given an integer mountain array arr of length n where the values increase to a peak element and then decrease. Return the index of the peak element. Your task is to solve it in O(log(n)) time complexity. Examples: Input: arr = [0,1,0] Output:
Binary Search Find Minimum in Rotated Sorted Array Solution In C++/Java/Python/JS Problem Description: Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: * [4,5,6,7,0,1,2] if it was rotated 4 times. * [0,1,2,4,
Find Peak Element LeetCode Solution | Code in C++/Java/Python/JS Problem Description: A peak element is an element that is strictly greater than its neighbors. Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks. You may imagine that nums[-1] = nums[
Binary Search Sqrt(x) LeetCode Solution | Approaches in C++/Java/Python/JS Problem Description: 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