# BinarySearch

Binary Search DSA.

## Find First and Last Position of Element in Sorted Array

[Question] 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. Example 1: Input: nums = [5,7,7,8,8,10], target = 8Output: [3,4] Example 2: Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1] We can …

## Find Ceil & Floor element in an array

[Question]: You’re given an sorted array arr of n integers and an integer x. Find the floor and ceiling of x The floor of x is the largest element in the array which is smaller than or equal to x.The ceiling of x is the smallest element in the array greater than or equal to …

## Search Insert position index

[Question]:  Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2 Example 2: …

## Find Upper bound: Binary Search

[Question]: Upper Bound || Given a sorted array of N integers and an integer x, write a program to find the upper bound of x. Example 1: Input Format: N = 4, arr[] = {1,2,2,3}, x = 2 Result: 3 Explanation: Index 3 is the smallest index such that arr > x. Example 2: Input …

## Find Lower bound of x

[Question]:  Given a sorted array  and an integer x, write a program to find the lower bound of x.(arr[i] >= x)) Example 1: Input Format: N = 4, arr[] = {1,2,2,3}, x = 2 Result: 1 Explanation: Index 1 is the smallest index such that arr >= x. Example 2: Input Format: N = 5, …

## Binary search: Find an element in an array

[Question]: Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4 Example …