You are on page 1of 25
21S€1202 - DATA STRUCTURES Week-6 ‘Time of the Session: sre given a stack of N integers. In one operation, you can either pop an element fom the tack o pak mj clement into the stack. You need to maximize the top clement of the stack after pecforming exactly — ing K operations and there is no other way forthe stack te scons. If the stack becomes empty: after performis # Prelude #Poclude Hdeftoe Sige 4 fot top = 1, Tob arwsay TsFae] void push) votd pop); votd Show (); fn mato () ¢ Pk chotce; uabtle 0 f Scant ("AA gchotce); Suwttch (chotce) $ Case 4¢ push0s break 5 Core 24 pops hyeak ; Case 3 2 Shows breaks 7" Gse Us Test(o), defauit ; prtole ("40 Sovalfel chotce”), { t } Noid push () Tok as it (bop = s%30-1) printe Cw» Over Pow"), ele : Scanp (* ua", Ba), Top + +; am Lropy ~4; 5 f Void PPD S ip (top. =.) Patou (eo, Sedevftow), I S@Dmis “4049 Wan)urnd Sy yunu swan, t C= wry) a 1.40 Tuas “1-=day Pro] 9209 305 $ (rv 312) ergopvp pyon t Co tama. (uma) 499327 Homes” py) dems seme 8) t (uous 24 queespris uy Sfeavwrgems yoy * dou gus # 1°z) wend yh F(xdeav-yo0ys yoy dey Hs) Radeon aoe op-waw 24y}eP <4-0\P}S> ap Pye) pnt stocks ar 3) vate pou (tats, OEE i * (Yep == cnne-d) f print e (* gkack overflow"); } tae f , bp -* bop tls Stack avy [top 2%, } 3 toe Pep ( tok * top, fot skack, acy ww) toe es *e( topes -t) a y Fete re overptows); catty. i 21SC1202 - DATA STRUCTURES. yp» ; you will be given queries, Bal te ye — AN be alte TURE ea ‘one of these theee type, are gent x into the at pa um erent inthe stack ee ackerrank. com/challenges/maximum-element/problem?isFullScreenine spy packernanscOT™ peo Htinclude d#octade typedef struck rede nodes struck-nede tong tong Tot data ¢ node * nents $5 nede * qekoode (tong tong tot dale) $ ode newnote = (nods) mnatlee (Stage s¥Cnadi)); Newneds > data -dala; Ne woods > nexk= NULL; rekdxn newnode: t node ¥ push (node ¥top tok tong 2099 date) ) mnallec (se node * oewnode © (node ™ Newneds data = top 5 crelunn, newnode; nde * detete (node * top) | nods temp. tops } yotd move (note ¥ om Eop + Sat datas from bP cletete (tem tof) 5 push (totp ifocte’) 5 wid protmat (node *martop)? priate (SZdan "mon bop data); chet ce; Leng tak NS Dools *kop -NuLL; Sconf ("7a 2N); whik (n)4_ tg tot data top=top° Pree (4emps Teturn Lops sHO4 Sot moi VE (top ==Now) roan kop = push [top dats); else ft (date> =maxkop) ‘oaakop = pash(may ep deta); top> posh (top, data); Stank (7a sche); Te onctee 1) f Scanf ("% tid Tnpat as Ydaka); 1% a eats else € ( pate totop) { a> dake; i thetce = =a) af IF ltop date}}= mort tap» dala) $ Tarikops delete (maxtsp), *tp=deletettop, else qf (chetce =D °, Patol max (top) - F ie } Thin gy 21S€1202 - DATA sri of cylinders where each cylinder has the same di sa atack by omeving aa discarding it opm ee hey yr i igh, possible height of the stncks such that all of the stacks ang oath ote, Y8 cr more eylinders from the top of zero cr mors ofthe tres gent MB Th at qhen return the height, sacks ‘hey area ee /ehall qual-sts is wt ooh enh. cmnsllongs/ena-stassroblenta renin 2. include estdto.ns #Pnctude ™ “Hinctude < math-h> fot fend max (tok b, fob bs, toe iE tink max, te (hie chy KE hy==hy) aeturn bi; mat = (hy rhg?ts2 + i (mares) mar? (hiehs)? 1235 mars Canes velum max; é ; cath, faths, tats, fol balance (fot *N, tok +s, tok Wp grtath, (OFM Sn bs) 4 Toe ka Ky 5 Kye ky eles =O; ME mane fted mat (n : woitle (L (neshe At hae z halls Ut mare Findenae [his M2" Fp cen ane ct) yer Ns Leeds prin aeh3)s teonae ©) het Na Cea 40) ” (nesalthsrol! h3* svelunn OF } t retave bas Tok mat a0)4 POE O,,n5, Mg, by a,Part 3 hishs =hy=es Sang (xd Xd Za 7 Anngtn,); int FN, =mallee (seep (Fot4}4n), bern. ‘ i nk FN = mallog (Sige of Cink*)q), mallse(st Ge opttaes}ing, FovCiaoste mss tu1) Sot Fas for (Snoring trays Stang fae7 ate Sean 07a", 24,08), "ae eS he=Mattl hetenelaa t fi (teoster 4 ae Che tos V4) Scang cs ha” ana tth), hye att, veluan oy Le petote ( Zdwn” by wEEEARS & 7 Ey 21SC1202 - DATA STRUCTURES ys y. Thar pile of numbered plates, Creat snmp any oir 27 ne tack in order. Determine if the mits con ee there sick Xen Sos ann oy aes 0p pe Ha the valu 1k. Once the pin ott te cm cpiotator Narn been jong ee challenges/waiter/problemtisFullSereen=true Hinctudecstdions fe ncdude egtrtngeh> dindude Zinctude ¥stdiibehs fideptne osack 2 ftdepine oph 0 tdegtoe op? t Fok gptsls ink * Stack BI, Ved Anke Cenk) 4 fou Cink feos Feat) f Stack EIY= Cink *) maline (Saeeh cto! A); Sphi=o, i } WH puchsCtat 1, tts) f , See (betty -y Foy peps Cink id $ Weta stack Crd E-= Neide Cdoge s() t Forint teoy Af wet) spt =) nae eM $ (rok 1) 4 eyeturn SPECT S rok OY for (fot toot feansteou ae (ny te zo’) sekomd; t met 43 Tok teprtene C s fnt nertpiime C fot ad ates enhile ( bitpvime (9) net 5 “reli 13 5 Fokmato() & WE vt, v, fos, oats: rienes Scab 7.4 %a fh ey) Fk There (aa, 21801202 - DATA STRUCTURES wootoxy, we use the Htack with samme meaning like stack of CDs oF sek of sou remove the CD at the top before moving CD below So ck first, We can make use of stack 10 reverse a given string, dito stack - letter by letter ~ andl then pop letters from the stick DVDs. Extend the (he CD which poe ‘To rere « word #fPacludecstdto.h> Afoctude cstdtib:by aHdifine mat too fot lop, Stack Cmax] ; Vold push (char) J 4 Ctop= maxi) dj panes ("Stock o ves flow"); 5 else { stack [i+ topT =x; 5 s veld pop () 4 Prio kd (* ye" shack Ceop-4), 5 tok rain ()) 2 chase sec -™ Sponge", fot Jan os buco (sts); Ye fo Po (iw) tate 7 Mad) i push (Se tra), 7 (Teo: Veten 94) we al Y oa Popa 5 218€1202 - DATA STRUCTURES iC wo input a two-digit mer and store ali divers nck, The og shoud Aincludeze tio h> # Tochude define MAX 50 sok Se tot top. tnt StackanLt3), Net peactor’ (tnt nar); Void push (fk x, tok ¥ bop, int Stack ava), ‘ok top (tot *top, tat Stact-arvbd), ok: enein() $ fot num: Seang (%%-4% daurn); peactors (num), i Void peactore (fot pum) } Yok Stack [Mad top -ii7 For tel yfemums itd) TP (num ty, t=0) 7 fush( 4, Bbp, Slack), 71SC1202-- DATA STRUCTURES ssotnve ts ed of reading Monk Gad “fea custom-made Bookshelf hs bint photornan ly from the top. Hooks are placed one mek o . los an algorithm. He first wri queries of either of the two types type of query Sudhanva pl wx.n) inthis ove of query Sudhanva places & book with hoof vehing in this query ‘ber Wo ton, You ay X-) his Query Sudhaava take out he tno Bole Sou any < shelf empty and Sudhanva i trying to take out « bay "umber on he book . ‘book, print "kuch? Widhaas pone stot Ft saorank.conrtalecese/ veers i Hincludecstdtoshs ttdefine MAx 10000000 fot bopas fot af Mayle fok push (fot 8)4 tops kop tis altopi- gs ek top c) { TF ( bop { prtokt (“kuchbht \o) else { points (“d\n", oT topl), topetop; 24SC1202 - DATASTRUCTURES gape poition ofan element inthe given stack of ump mi contain an intogst N, denoting the number of elements in sack, ail var contain elements starting from bottom and ending at top z will Nines yes" an integer X, whose deepest position is to be found, conti _ te" contain | Tine, printing the deepest index (| based inden) value of x asrov! ee packerearth. comiproblenyalgorithm/simplesstack/ rr Hfnclude '4!) « ager; ahtle (C>-0l be cng) § n=p*worc- to's e-gere; Yeturn ng votd pub-_ltnt (tong 44 chao te) f (o= 1) 9 Pck Co’) 5 pox Cie), velarn s elee ff Cites) 8 ex ue 4 b Chay Stouy 5 auto Fdreo 5 Sov (it, fd cae) Stand eet A eIey tle los fev auto tx# TAE-1i Re 9207 « pox (stand); Fe (le) por C10; retuvns 3 foe matin () 4 fong N < get. Lak; Bong k =gek. Yok); ling kodd = k% 9 hong kiax >t; Tr (1: Nas kead) pat-Lot Uk may,o), elu, Os 3 ip (kon) $ for (auto Miso; ten: TED: tong mum get. tok), ; (nur > max) Kean = nums elte For(auts nt =o, ie santa) gf ac num = get Atak); TE (kodds nize) te 4 (rum > kena) Kacey Pat LOEC kero i ' “lung, 1) = a + 4 A oie 248C1202 - DATA STRUCTURES mer 16! CaS, THeTeAter ah tet cay . Hn numberof commands and he stack at any potato uh intger in the stack; “pep meme 1 nieslanAlsrihn/akcnteeas re Oa aa tt inctudecstdforhy tot mato C4 tok qs seanf (* 7-4", 44); will Cy -9§ Tok x.) bop= 5 Scarf (Kal ky); Sean (¢%a" ka); fot ated; while (1-4 char Co]; scanf (“78% 0)5 char cele] =” push, chav ch[aq= “pop "s te (ste crm C¢,cc) >~2)} tok bs Scans 74% &5)5 if (top> = 44) Prtoke (stack overflow"), the alt+bopteb 3 t the f 14 Coecome Cog) ==) 9 z i (5, eS AE Seo De ans © SiS break } i W (Se 2ss At $9°S)P S,ealeds Port; pie te (S79S3 22 Gr=5)} $d [pa]; Pitty ese # (Sarg 42 sy=i)J S&S — 4 Sncludee Tosteam > wfoclude cqurit> catng namespace stds caw Solutten 2 Std s stack moyskack; sd rquuue < thar my quae pubitcs Vet d pushcharacter (chow ch) f m4 stack «push (ch), 2 f void engusut chewacles (char oi) § mys ars poe char pepchavacter€) 4 Chor top = mystack stop(); musraclere Rey “teturn bop; Chay dequiut hovacle 1OL char front. my qn” frontd) 5 my rut pop); ) ha ae st matol)f ghtag 83 getttne (e093 “sotubton Ob} for (tok Seo; hh Ly Oe obi. push characky (set), eh} eng chovacker (su), 5 boot Spaltadrome = fur, fe(int 05 tes Leng thr / rey % Cony epchoracles()! obs, iol palindromes Palie, breale» } 5 TEC palin derma) j = ‘The Word"! £2820" Tg palindrome », ai cet (Cour

You might also like