# BinarySearch

Binary Search DSA.

## Median of Row Wise Sorted Matrix

[Question]: Given a row-wise sorted matrix of size r*c, where r is no. of rows and c is no. of columns, find the median in the given matrix. where – r*c is odd Example 1: Input: r = 3 , c = 3 1 4 9 2 5 6 3 8 7 Output: Median = 5 …

## 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 …

## 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 …

## Find the row with maximum number of 1s

[Question]: Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.

## Minimize Max Distance to Gas Station

[Question] We have an horizontal number line. On that number line, we have gas stations at positions stations, stations, …, 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 …

## 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 …

## Allocate Books || Binary Search

[Question]: Given an array of integer numbers, ‘arr[i]’ represents the number of pages in the ‘i-th’ book. There are ‘m’ number of students, and the task is to allocate all the books to the students. Allocate books in such a way that: 1. Each student gets at least one book. 2. Each book should be …

## Aggressive Cows : Detailed Solution

[Question]: Given an array of length ‘N’, where each element denotes the position of a stall. Now you have ‘N’ stalls and an integer ‘K’ which denotes the number of cows that are aggressive. To prevent the cows from hurting each other, you need to assign the cows to the stalls, such that the minimum …

## 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: …

## Capacity To Ship Packages Within D Days

[Question]: A conveyor belt has packages that must be shipped from one port to another within days days.The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.Return …