# BinarySearch

Binary Search DSA.

## Find the Smallest Divisor Given a Threshold

[Question] 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 smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of the division is rounded to the nearest integer greater than or equal to that …

## Minimum Number of Days to Make m Bouquets

[Question] In an integer array bloomDay, an integer m and an integer k.You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden. The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet. Return the minimum number of days you need to wait to be able to make m bouquets from the garden. …

## Koko eating Banana || Return min value using Binary search

[Question]: There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas …

## Find Nth root using Binary search.

[Question]: Given two numbers N and M, find the Nth root of M. The nth root of a number M is defined as a number X when raised to the power N equals M. If the ‘nth root is not an integer, return -1. Example 1: Input Format: N = 3, M = 27 Result: …

## Finding Sqrt of a number using Binary Search

[Question]: Find SQRT of a number using binary search.Example: Input: x = 8. Output: 2

## Find out how many times the array has been rotated || Find Lowest Element Index

[Question] Given an integer array arr of size N, sorted in ascending order (with distinct values). Now the array is rotated between 1 to N times which is unknown. Find how many times the array has been rotated.  Example 1: Input Format: arr = [4,5,6,7,0,1,2,3] Result: 4 Explanation: The original array should be [0,1,2,3,4,5,6,7]. So, we can notice …

## Search Single Element in a sorted array

[Question]: You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.Return the single element that appears only once.Your solution must run in O(log n) time and O(1) space. Example 1: Input: nums = [1,1,2,3,3,4,4,8,8] Output: 2

## Find Peak Element in an Array

[Question]: 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. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: …

## Find Minimum in Rotated Sorted Array

[Question]: 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,5,6,7] if it was rotated 7 times. Notice that rotating an array [a, a, a, …, a[n-1]] 1 time results in the array [a[n-1], a, a, a, …, a[n-2]].Given the sorted rotated array nums of unique elements, return the minimum element of this array.You must write an …

## Search in Rotated Sorted Array

[Question] In a given array which is sorted in ascending order (with distinct values). return the index of target if it is in nums, or -1 if it is not in nums. Example 1: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Example 2: Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1 #Approach: Using Binary search We can find element using binary search, we …