Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
100%
(1)
100% found this document useful (1 vote)
515 views
26 pages
Binary Seach Aditya Verma
Adiya verma notes binary search not writeen by me
Uploaded by
sayantanchakrabarti18
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save Binary seach aditya verma For Later
100%
100% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
100%
(1)
100% found this document useful (1 vote)
515 views
26 pages
Binary Seach Aditya Verma
Adiya verma notes binary search not writeen by me
Uploaded by
sayantanchakrabarti18
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Binary seach aditya verma For Later
100%
100% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 26
Search
Fullscreen
Binary Seayven + Aroay 's Sovted avy C) ot 2 3 ie See pee 5 108 8 findele © 2% CRamcet). oe ae Ses Return Tndexa 2 [aise Obs mes) Smalley Ahan Greterv T i mid ve Hhan mid end am mid= Stexttend = pase, a = Our target lies in Left of mid eoil now Check Left part nO: @nd = mid -1 [2 ene? fis Stont 4 Er! Mids SnGeaeay Oca Se yes! =) | “3 az Wwe Found answer: Prog: Us; : a Using Binary Search We eliminate Bat Fin tt SCS) of Ber] Teenof™. Comp = '0q.9| ,peeudeC one — Teper co int end = Size-1; While Cs tart Ze end) < int wid = starvt tend 2 if Cele == ary Cmidj) Yeturn mid ; else if Cele < axv Genre end = mid-i5 else Stavt = mid +15 yeturn -1- Ii% we donot gef elem a Note : Fancy Way to waite mid- Int | mid = Sbevt + end-start a Why 7D if start G end atave too bige Sey 109 then anint mid = Geen - 7a will oveyflew the yange of int.Sy Descending Sovted Arevasy Mec): 20 17 15 i4 13 12 10 9) some target 14 Be Sen Intuition - Artereuy is Sovted in cvevevse ovdey — Se the change thet we voll domes move to right if [mid] < > tovget move to left iF [mi] < target A eee 9 1T 1s wy 13 ie 3° 3) 3 oa Cl : ood * i Start mid = § ee a arx Lmid] 7 target | $0 moveto Tight - Start = mid +1 & ‘ 7 3 zyion® 12.8 8 t , ) S mid e = WS + 3-5 z 2 ave Cmid) 7tav get Start = mid+\ Teevation 2 3 3 e I ry Lrnid] 7tayge ! 1 S)mid e Stovt = mid+] mid = 3 + 49-8Pseudo Code: int Start =O int end = Size -) while C stayt <= end) int mid = start + i Carry [mid] == tag teturn mid; else if Cary Crd > tora Start = mid +35 else, ‘ end = md -} i return —\ 5Figrst and Last 4» O " of Element. lo 1 oA luc, i Nowe: 2 Te! Pied ~ Payee Sicc eee 7 We will Cismaroerr le, 7? When in (Sam We will get the el We will stove it in and Chonge, = errlaee mid - > This change WNL Find the the element mid aa IS present Wele= anaes not: And 9 Element is Found change Value of Variable lueey index: ey ? To Find Last Occuvance | 7 Rest all “things will be sa as finst Occuvance , just on thing will change, * instead of “end we will 4 ' Change. tstervt -omid +1 | “80 We will have to make 2 While Loop ™e for Pivst ocurance 6 other fy eee a O(log nyPseudo Useugo Code \- int start =o int end = Size-| int First Occ = - While (gtavb <= ee 5, eae Ctorget == avy[Emid]) First Oce = mid @nd = mid-i elseif ( target < avyEmid}) end = mid-J Ee Stayt = mid+) 3 int stavt =O int €nd = size-| int Last Oc = —1 While ( Stayt <= end) t iF (target = ory CmidJ) LastO« = mid Stayt = mid +1 else if Charge? < avatmid Dy} end = mid-\ else stark
found Pivst Oce & Last Oco in Previous Problem. So, Count = LastOcc - Fist Oce +1Loe Gy How many times A Sorted Array Is Rotated - ove a leis 1 7 Sie quee Sntuition : Novmal 9 t Ce i ie ‘a 7 aap fa]}sle 3 ]u]i2|is ['8) min ele index 20 Ree esta Set on: SS Pee in, [pe yisfiete[s c[2 | J min ele index = 4 menos time — Trdex of Pee) rin cle | ae) ele So if we find Min, Index de We will Fed th get the Ans %® How tocheck ele is min pay net in te 61S) = 5 (ae 7 ele is a Smalley than @ Both Neighbour ary [mid-1] > arvfmid] < aryv{mid+] | —@x min cle wit ba always present Ue part of array « ex = (a 2: | ee 1 ; : Start mid- ele Sill Sorted. Also Note: start ele IS greater [| —— © than mid ele- Algo: © Appy normal binovy search ® Find mid @ check mid is min oy not Oar Emmis 1e<: 5 40% Comid +) 2 ae Next avy Emidl] < avy [mid +N-1) XN] oNEaed 1€ true veturn Teakex mid - @ rf false: Move to ungonted part of ary igi @ors (store 5 cal CmidJ ) Stort = mid +1 3 elae if ( ov7> (risa) ae cend3) end = pale eae«ah Pseudo Code :— int gtavt = 0 int end. izes while Cstayvt <= mid) t int mid = start + end start a2 int Next =(mid+!) 7N int Paev = (mid+ N-!) JN if ( ary Cmid]}
jo 2°e Scum Tntution:— sorted word is used so we will Use. Binary searh for sure: BS Modified Bs (Sosted ) (Nearly Sorted ) er - end. mid stet end Start | Se i a end =m es =mid+! onl 4 ont = Ne ete cmid-2 Mele tm index i an 141 index if (ele == ary [mia]) Jif Cele == ony mid) elseif +__— = aa) (ele == ary (mid -) (ele == ary Cmid +i) To move left 2 ae . end =mideal Teo move Right Tomove Rignt Start = mid +} Start = mid +2Teeude Code , int Left <0 Int Right= Size-\ while (Left <2 Rignt)_, fecois ioe if Cele == a(midJ) dua mid FC mid r= stort $4 a[mil-i] == ele) creturn mid-! if Crmid é= end 4 ov [mid+i]= = ele) qeturn mid+) if ( ele < ary Cmid7) end = mid -| else stavt =mid+)By Find Flooy OF Element In Sorted Paes Bagel: 0) 36" ge jo) fon ce mie = 5 — Fleoy of 5 = 5S ov greatest element less than S. Battion:-O.2¢ will opoty BG. fow the ele- @¢ if the ee mid is less than ele Se we will stove it In @ variable- @ When We will be out of while loop. Vaviable will have our ans. G Sie toe Soa s ‘6 eS Example : eae | yo | dean mn O°” Oech i tT iS mid én} OMS ari{mid] 7 ele. : Move left. Saet li : | 1 3 4 5 mid 3 any (mid J < ele. ie potential Candidate Foy Sion GNSS ]T2\q - Move Tight - avy C3 2) avy Cmid)
D 3Orer Oey : S35€, mid - avy Cmid]
ele) Ons = arva{mid]; Stayt= mid +15 else 6nd = mid-1; Vetuan SNSQy Find Cell oF Clement in Sorted Array av (J: peta aghe iS 16 Ne bai iS ee. 5 _ [ext of 5 = Smallest cle Gredeqi nanmee (ola Equal to £ Heve the potertal candidates ave 8 1010 1219 Srnaliest among them ““.e. 8 coll teauencreee Entuitien ; Just one change From ta Lloow problem. we will Stove the ele gteatey than 5 in ans variable. feadatode .- While Ci start <= end) Fe iF Cary emid) -= ele) Yeturn anid. Ova Emid]: Ase F C awmCmidy < key) Start = mid +1 ; else Ans = ava Cit d ] end = mid-1; aeturn Ans,LO: Gi Next Alphabetical Element OnE]: «a Wy omen et q ib, key =f, We need to-~veturn | next element ia ooray) This que is Similay §° find Ceil discussed previeushy- Bik Key So when we will find ef in 8S te then web will metumm next index of Prd if ee cle is greeter than Key stor Pseudo Code, a, it in © Variable - while ( stort <= end) iy cy oe = Lif Cary fmid] == key) Meturn mid+i; else 1f (any Cmidd < Key) Stayt = mid +15 else Ans end mid mid -| i, <4 3 cretury Ans;4) Find Position OF Ele in Infinite } Sovted Bomrays BicJ° '? 34s 6 7 § ae te Sa 2? Diffevence bet” nogmal 8S and ether this pvoblem iS we dont have end ele. “ We cannot do- int end 5 see 6S Modified 8S. Ooo 0 Oo OOS oe r oon ¢ stort end Start end 99 We dont Know end- x How to find Wend index 2 — > tets mark end index at start tl position. PPO Oo... it Stat end Example : Key =7 % ' a 3 4 s c 7 s 4 Re me 345 6 7 8 9 jou im ft Start ond Now Compare, “key? and ‘are Cend J if Key 7 avy Cend J Le Tap ge Start=end g end = end** ¢Sree!” 3 Algo :- key > brent) S=6 Quer > jeomae ys co we 3 T fe Ss eC Key 7 are Cend] Gae aes Mee 4 5G Ju te i ie e key < aaa Cend] Apply RBS on (, e) @ initicde ~ stavt =o “end =| @iké (orn (end) < Key) Start = Bend @nd = end #2 @ Now qany fend] > key Apply BS on ( Start > endl) of aro) «Pseudo Code :- int start =0 ine endel While ( Key > ave [end] ) & Start = end; end = Crndi * 255 BS (Oc, sche y end);ESI Sy Find Index of ‘1’ in Binary Sorted Array. Avaay i§ 0. Sl : @his “ausin. ombined” of Sexrch acon and Find st occurance. Pseudo code: int gtart <0 intend =1 While C Key 7 avy Cend3) () Canes) & start send; @nd = End #2 int Fixst -0C6 =-15 Remen@sen: <= end) _miaeStPE) Be Cerget = od) Piyst oc = ravd fais end = mid—15 Occuran ce else # (target < avy Emd]) end =mid-1} else i stavt =midt) | 5 aduen Finest Oce 5MM. Tyee ect enone Aen yy Mimi Difhewence Element im Sorted Arvay oy Minimum mei at Ge AO) key: 7 _— 4) | 6 io | er gee aL aa 3 (aes mae a Min diff if Key iS present LF Key is not present Key = \2 fa [712 1/3 8 | to] sig J 7 a jo: {12 TOS Sa ose ce Ss ae BO): 3) Mere 2 3 il niin min diff dH is giver oy ct Apply Simple BS neorest elernern § Return ele. of a: a * How to fing them 7 Tf we apply BS and key is not present gtaxt | erd will point to neavest elements: . 1 Q 3 4 \ 3 3 10 15 Key = 412 t r i S m fe « 5 4 % Oo aas (gz @)Pseudo Code :- Apply BS qeturn er (a key mSI Peak Element amt): § [ORO Ome 11S) Bee Preak element Is one greater thon its Neighbours - Ex. 16 wm I6S5 10 < a6 XK not possible - Ex: 20 > 15 = Qo § Pear Element Door N10 Breas! 10:1 9-0 Ay. AO. So So > lo Yit last element i “+ Peak. Sienilanly Gay tJ: SO 4o 20 30 40 i Peak ele Antuition --9 while performing BS check mid with mid +1 & midi jg mid is qreadey than both mid is peak @ IP mid is not peak Then theve iS charce thot ele nearby an greater than mad can be Pear - ICME Grolcl ees el es Ne chance & mide-l voll be pear. mid < mid+1 e _ Chance of mid +1 Can be peas - Move Right -—— * otart = mid a Sroid < noel end =mid -1; Basically Jo bhi sbadesPr 0 Code: bow = O grease while ( low <=high) t int rod = low + [ughleo) pe jaa 2 (mid zoo ee Sree) element a if Ce mid] 7 avy{mid-1] gery Timid] > avy Emid +i] yetumn mid; Clse if Cary Emid] < ary [mid-)) high = mid -| else low = mid +15 3 } else if (mid = =0) Lif Cory fe] > avy £19) return 0; else aeturn 13 3 elge if (mid =< size-!) rk iF Coma Csize- i) 7 av (size-2)) yeturn Size =1; e\se aeturn gite-2 5Gy Search In Bitonic amv]: “| 9S) sane aa Key 2 4 ——~ Intuition !- | 93% ¢ 3) Osman =. a Gers peak This Q is combinasion of Zep © Finding Pecuc ® Seay ch in Sorted array ® Search in Descending 5 Res (4 Sovted Avvay Apply these 3 problems 'e S while lox Te= o(legn)
You might also like
Graph Hand Written Striver Notes
PDF
No ratings yet
Graph Hand Written Striver Notes
119 pages
DP Notes Aditya Verma
PDF
100% (1)
DP Notes Aditya Verma
104 pages
Neetcode 150
PDF
No ratings yet
Neetcode 150
13 pages
Algo Zenith
PDF
No ratings yet
Algo Zenith
28 pages
Notes (DBMS) Knowledge Gate
PDF
No ratings yet
Notes (DBMS) Knowledge Gate
85 pages
NITK Placement Gyan 2014 PDF
PDF
No ratings yet
NITK Placement Gyan 2014 PDF
216 pages
Striver Codes
PDF
No ratings yet
Striver Codes
199 pages
00 Linked List Handwritten Notes
PDF
No ratings yet
00 Linked List Handwritten Notes
22 pages
Striver Graph
PDF
No ratings yet
Striver Graph
21 pages
DSA Sheet Final - Google Sheets
PDF
No ratings yet
DSA Sheet Final - Google Sheets
9 pages
Trie Notes of Striver (TUF) Playlist
PDF
No ratings yet
Trie Notes of Striver (TUF) Playlist
24 pages
Striver Graph Notes
PDF
0% (1)
Striver Graph Notes
37 pages
DBMS Short Notes
PDF
No ratings yet
DBMS Short Notes
4 pages
TCS NQT 4
PDF
No ratings yet
TCS NQT 4
23 pages
Data Structure Practicals
PDF
100% (2)
Data Structure Practicals
29 pages
Striver's CP List (Solely For Preparing For Coding Rounds of Top Prod Based Companies and To Do Well in Coding Sites and Competitions)
PDF
No ratings yet
Striver's CP List (Solely For Preparing For Coding Rounds of Top Prod Based Companies and To Do Well in Coding Sites and Competitions)
30 pages
CP RoadMap (Personal From BU)
PDF
No ratings yet
CP RoadMap (Personal From BU)
9 pages
DSTL Unit 5
PDF
No ratings yet
DSTL Unit 5
5 pages
Josh Coding Questions
PDF
No ratings yet
Josh Coding Questions
7 pages
TCS Digital Previous Paper 1
PDF
No ratings yet
TCS Digital Previous Paper 1
16 pages
Preparatory Guidance From Infosys - SP and DSE Roles
PDF
80% (5)
Preparatory Guidance From Infosys - SP and DSE Roles
9 pages
English Dice Questions For SSC CGL Set-2 PDF
PDF
No ratings yet
English Dice Questions For SSC CGL Set-2 PDF
5 pages
Recursion
PDF
No ratings yet
Recursion
15 pages
Solutions Zake
PDF
No ratings yet
Solutions Zake
112 pages
Code Tantra Dsa Aktu
PDF
No ratings yet
Code Tantra Dsa Aktu
60 pages
(Nonlinear (6-31) : Structures GTU-Sem. 3-Comp/T) Binary Tree
PDF
No ratings yet
(Nonlinear (6-31) : Structures GTU-Sem. 3-Comp/T) Binary Tree
25 pages
Javalab File
PDF
No ratings yet
Javalab File
167 pages
Complete Roadmaap
PDF
No ratings yet
Complete Roadmaap
2 pages
TCS NQT Roadmapss
PDF
No ratings yet
TCS NQT Roadmapss
7 pages
Dashrath Nandan JAVA (Unit2) Notes
PDF
No ratings yet
Dashrath Nandan JAVA (Unit2) Notes
18 pages
DPP 6 TCS NQT PREVIOUS YEAR QUESTIONS by Knowledge Gate Yash Sir
PDF
No ratings yet
DPP 6 TCS NQT PREVIOUS YEAR QUESTIONS by Knowledge Gate Yash Sir
8 pages
DSA Python 3.0
PDF
No ratings yet
DSA Python 3.0
12 pages
Coding Questions 09-11-2024
PDF
0% (1)
Coding Questions 09-11-2024
5 pages
Wipro Coding Questions
PDF
No ratings yet
Wipro Coding Questions
11 pages
Recurrence-Relations Time Complexity
PDF
No ratings yet
Recurrence-Relations Time Complexity
14 pages
DTU B.tech 1st Year CO LAB FILE
PDF
No ratings yet
DTU B.tech 1st Year CO LAB FILE
119 pages
Gate DBMS
PDF
No ratings yet
Gate DBMS
164 pages
PS2 2024-25 Station Allotments
PDF
0% (1)
PS2 2024-25 Station Allotments
154 pages
Gate - DS & Programming
PDF
No ratings yet
Gate - DS & Programming
211 pages
Search and Sorting
PDF
No ratings yet
Search and Sorting
11 pages
Tree Striver Notes
PDF
No ratings yet
Tree Striver Notes
46 pages
Walmart Most Asked Question On LeetCode
PDF
No ratings yet
Walmart Most Asked Question On LeetCode
4 pages
Curious Freaks Coding Sheet
PDF
No ratings yet
Curious Freaks Coding Sheet
7 pages
Striver Graph Notes
PDF
No ratings yet
Striver Graph Notes
37 pages
Striver Recursion Notes
PDF
No ratings yet
Striver Recursion Notes
52 pages
Striver SDE Sheet
PDF
No ratings yet
Striver SDE Sheet
8 pages
Decode Java With Dsa Pwskills
PDF
No ratings yet
Decode Java With Dsa Pwskills
14 pages
12-Binary Search
PDF
No ratings yet
12-Binary Search
3 pages
Binary Search AdityaVerma
PDF
No ratings yet
Binary Search AdityaVerma
15 pages
ADA Unit5 PDF
PDF
100% (1)
ADA Unit5 PDF
12 pages
Leetcode DSA Sheet by Fraz
PDF
No ratings yet
Leetcode DSA Sheet by Fraz
20 pages
DSA-251 by Parikh Jain
PDF
No ratings yet
DSA-251 by Parikh Jain
23 pages
DSA Patterns Sheet Kushal Vijay
PDF
No ratings yet
DSA Patterns Sheet Kushal Vijay
2 pages
Chapter 8
PDF
No ratings yet
Chapter 8
27 pages
DSA-251 by Parikh Jain
PDF
No ratings yet
DSA-251 by Parikh Jain
22 pages
CP RoadMap (Personal From BU)
PDF
No ratings yet
CP RoadMap (Personal From BU)
9 pages
Compiler Design Practical File PDF
PDF
No ratings yet
Compiler Design Practical File PDF
33 pages
TCS NQT Prep Sheet
PDF
No ratings yet
TCS NQT Prep Sheet
4 pages
Pseudo Code Capgemini Comprehesnsive Material by OnlineStudy4U
PDF
No ratings yet
Pseudo Code Capgemini Comprehesnsive Material by OnlineStudy4U
25 pages
DSA Preparation Syllabus - PrepInsta Prime
PDF
No ratings yet
DSA Preparation Syllabus - PrepInsta Prime
1 page