BinarySearch

Binary Search DSA.

Find a Peak Element (Find Peak Grid in Matrix)

[Question]: A peak element in a 2D matrix is an element that is strictly greater than all of its adjacent neighbour’s 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][j] and return the length 2 array [i,j].You may assume that the entire matrix is surrounded by an outer perimeter with the value -1 in each cell. Write an …

Find a Peak Element (Find Peak Grid in Matrix) Read More »

Search a 2D Matrix II (Sorted Matrix)

[Question] : Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties: Example 1: Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5 Output: true Why Binary Search is not useful for searching in unsorted arrays? The basic condition to apply Binary Search anywhere in any algorithm is …

Search a 2D Matrix II (Sorted Matrix) Read More »

Minimize Max Distance to Gas Station

[Question] We have an horizontal number line. On that number line, we have gas stations at positions stations[0], stations[1], …, stations[N-1], where N = size of the stations array. Now, we add K more gas stations so that D, the maximum distance between adjacent gas stations, is minimized. We have to find the smallest possible value of D. Find the answer exactly to 2 decimal …

Minimize Max Distance to Gas Station Read More »

Split Array Largest Sum

[Question]: Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized. Return the minimized largest sum of the split. A subarray is a contiguous part of the array. Example 1: Input: nums = [7,2,5,10,8], k = 2 Output: 18 Explanation: There are four ways to split nums into two subarrays. The …

Split Array Largest Sum Read More »

Find Kth Missing Positive Number

[Question]: Given an array arr of positive integers sorted in a strictly increasing order, and an integer k. Return the kth positive integer that is missing from this array. Example 1: Input: arr = [2,3,4,7,11], k = 5 Output: 9 Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,…]. The 5th missing positive integer is 9. Example 2: Input: arr = [1,2,3,4], k = 2 Output: 6 Explanation: …

Find Kth Missing Positive Number Read More »