0% found this document useful (0 votes)
15 views8 pages

VG Frameworks Binary Search

Binary search

Uploaded by

nazmul nabil
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)
15 views8 pages

VG Frameworks Binary Search

Binary search

Uploaded by

nazmul nabil
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/ 8

AlgoZenith

VG Frameworks
Binary
Search
Key Concepts
Fundamental Problem & Steps
Code Template
Binary Search Frameworks
FAQs

SWIPE LEFT >>>

VISIT FOR MORE INFORMATION


www.maang.in/learn
Key Concepts
An optimized approach of searching
an element in a sorted array.

The basic idea is to look in the middle,


compare with the key and search.

But wait that’s it ?

Well not really Swipe Left to know more


about the Binary Search Algorithm.

VISIT FOR MORE


SWIPE LEFT >>> www.maang.in/learn
Fundamental Problem
Note: Any Binary Search Problem can
be mapped easily into this
fundamental concept.

Find the index of the first occurrence of


1 in an array consisting of a sequence
of zeroes followed by ones.

index-> 0 1 2 3 4 5 6 7 8 9
values-> 0 0 0 0 0 0 0 1 1 1
ans = 7

VISIT FOR MORE


SWIPE LEFT >>> www.maang.in/learn
Steps
1). Identify the Search Space, possibility
of getting an answer. In most cases its
Index. For this problem the search space
would be [ 0 , N-1 ] ; Set lo=0, hi = N-1

2). Maintain an ans variable with initial


value -1 as there might be a possibility
that one does not exist in the array.

3). Find mid=(lo+hi)/2. If A[mid] == 0,


discard the left side by updating
lo=mid+1. The ans will be within the
updated range if it exists.

VISIT FOR MORE


SWIPE LEFT >>> www.maang.in/learn
Code Template

Note: If you do (lo+ hi)/2, there is a possibility of integer


overflow when low and high are large integers. This
can lead to incorrect results or we can say undefined
behavior. that’s why we do mid= lo+(hi-low)/2 ;

VISIT FOR MORE


SWIPE LEFT >>> www.maang.in/learn
Binary Search Forms

B.S. on answer B.S. on every start

90% of B.S. Qs in OA’s 70%-80% Qs can be


from this topic. This easily solved using
is not the usual Find two- pointers
the index problem concept.

Atomic Items Contribution

Sweep Coverage Note : The way


you design your
2-D Variations check functions
will get cover in
Miscellaneous these forms

VISIT FOR MORE


SWIPE LEFT >>> www.maang.in/learn
Course Available for Free!

Master Binary Search Algorithm with


our comprehensive course! Sign up
today at https://www.maang.in and
unlock everything you need to know
about one of the most important
topics in tech. Don't miss out!

VISIT FOR MORE


SWIPE LEFT >>> www.maang.in/learn
FAQ’s
Design a lower bound function
Rotated Sorted Array
Bitonic Array
Sqrt(X)
Minimize the max Neighbor distance
Famous Painter Partition Problem

LeetCode Questions
275. H-Index II
367. Valid Perfect Square
441. Arranging Coins
744. Smallest Letter Greater Than Target
1198. Smallest Common Element in All rows

VISIT FOR MORE


www.maang.in/learn

You might also like