0% found this document useful (0 votes)
41 views23 pages

Binary Search SIG-ASN

The document discusses binary search, an efficient algorithm to search sorted arrays. It works by repeatedly dividing the search interval in half. The complexity is O(logN) compared to linear search's O(N). Binary search requires a sorted array and can find positions like upper and lower bounds. Sample problems on square roots, arranging coins, koko bananas and angry cows are provided.

Uploaded by

Saneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views23 pages

Binary Search SIG-ASN

The document discusses binary search, an efficient algorithm to search sorted arrays. It works by repeatedly dividing the search interval in half. The complexity is O(logN) compared to linear search's O(N). Binary search requires a sorted array and can find positions like upper and lower bounds. Sample problems on square roots, arranging coins, koko bananas and angry cows are provided.

Uploaded by

Saneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

BINARY

SEARCH

Introduction &
Implementation
Q. Check if a value is present in the array
Linear Search : sequentially checks each

element of the array until a match is found

or the whole array has been searched.


Binary Search is a searching

Binary algorithm used in a sorted array

by repeatedly dividing the search

Search interval in half. The idea of binary

search is to use the information

that the array is sorted and

Divide & Conquer


reduce the time complexity
Example : Find if 23 exists in the given sorted array
Approach

Iterative
Recursive
Iterative Method
Recursive Method
Binary v/s Linear
Binary Search is the most efficient

searching algorithm with run time

complexity of O(logN) while

Linear Search has a run time

complexity of O(N) where N is the

number of elements present in

the array.
Binary Search can only be

applied when the array is sorted.


Let's solve

some

Questions!
Q 1: Upper & Lower Bound

Implement lower bound and upper bound using

Binary Search.

Theory:

Upper bound is the first element in a certain group of elements

which is strictly greater than a certain given element.

Lower bound is the first element in a certain group of elements

which is greater than or equal to a certain given element.


Q 2: Square Root
Find the square root of any number using binary
search.
Theory:

Square root of a number is a value which when multiplied by itself

gives back the original number itself. Binary search helps us to find

the square root in 0(logN) where N is the number whose square root

is to be found.
Q 3: Arranging Coins
You have N coins and you want to build a staircase

using these coins. The staircase consists of K rows

where ith row consists of i coins. The last row may be

incomplete. Given an integer N, return the number of

complete rows you can build.


Q 4: Koko and Bananas
Koko loves eating bananas. There are N piles of banana, where ith pile
has piles[i] bananas. The guards are gone and will come back in h
hours. Koko can decide the speed at which she eats banana per hour.
Every hour, koko chooses a pile of banana and eats bananas of that
pile. If the no of bananas in that pile is less than her speed of eating
them, she wont eat a banana from any other pile for that hour. Koko
wants to finish every pile before the guards come. Find out the
minimum speed with which she should eat bananas before the guards
arive.
Q 5: Angry Cows
There is a new barn with N stalls and C cows. The stalls are
located on a straight line at positions x1, x2, .... xN, (0 <= xi <=
10^9). We want to assign stalls to the cows such that the
minimum distance between them is as large as possible. What
is the largest minimum distance possible. (Only 1 cow can be
kept at 1 stall)
Doubts
Thank you
for
attending!

You might also like