Partial preview of the text
Download DSA Essentials Part 1 and more Study notes Computer Applications in PDF only on Docsity!
a RAISONI GROUP Raison GROUP ve _ | Given an 'mxn’ integer matrix. if an element is "0 a set the entire row and column to 30. q You must do ft fn olaace : -1 element 3 tO” a _ —__Gomplenity =~ (ole) x (+m) a _—Spate_complenity OC) — of | ¢ = ® MS © RAISONI GROUP — a vision beyond — _ @] doscals. Trianale:- retumn the first numKaws of Given an integer “numRows " pascal triangle - t J o™~ ; num Rows =5 7 Vd ' Output = Cd, a Ardddd “pu, = Ci2'] CVvVUG fi.3.3,1] aver ee | i (CTEt in interview , they = a Approach :- sd ask whad is the valu 7 1 (Present af 2 vow $ CH clump Another Subpmblem ING: 4 ID ple: 2 Sy 4 374 clumnd (6 (| pools Piande = print only One f the Tour from A te formate | Uc Cay ae | ann S row should be | printe ww, Ul EET Z Aw fer the original _ ie > ue it problem. J ee == AH We. Gan oes the vector to (ii | then fev (i=0 > ic nwm Raw 3+ _ons/ifo =ansfifiJ= 1 | ny _ _ | __fer(jets psi tty) ge fost lum fayt-column Clelerout element 7 ai — _ons (ie ans{i-t} Fj= J+ans [1-0 [pete oor @) Next Permutation ~ Array = [1,213] then its permutations ave [1.203] Ci. 3.2) then next permutation [3.12] for Cia 3) > [Ga] [* rai | Brute - A - Apporach Generate All possible combos (permutations) fren find the nums am eturn its next permutation if Jat nuns is found then the first penmut ation will be ts next permutation and Opies Appro ach:- Ainearly traverse from backwards and we have to traverse til we se vai nxt Fer” ari ont iss 4 joazses “72 3 ined (| ‘ ol gd Step_ js Trosexse- el Linear tavern! fom bak and find len which ts a Greater than the value at index =| thin we hove get index 2 = 3 “Swop Cinder mr cep Swap (arr {ind art], anr[indlex J) S145 32 PPP PPP HNOMUOHMOKEKE EKA. wn Step :- . oO Yenewse( index itl , font) 7 FFime complerciy 9 O(n) » ) Ups re) Ww 5 4 t an array of OS ,J4S oloTs [ieetcode)] Given an om ‘nums’ with n colored objects a sort them in-place So that objects of the same with the colors in te order Yed, white § blue Ek -- ~ Tnput nums = [218,21 blo] output = [0.0, 1/1/22) Approach ‘This problem can be Solved b using Dutch Natonal flaq Al onthm Ae a All the no from pe]+TsJelils [P12 tole (o.. dow-1] are “Q" ynid hugh : : wt ) [high +1 -- 0] ) are 2" Nous shuft mid pointer if mid pointer 5 — —> then (soo sert[iow J, anr[mid) | points lowt+s mid++4+ ; if mid pointer 3 4 — then mid+t 3 poind if mid pointer 3 2 — then j swap(a (mid, ove (righ) fen hign-- > s ved, whit or blue calor are adj aceut BAAAAANA Aba a oo — op em ® Dayo a Oo 1®@ Rotate _ Image [Medium] © RAISONI GROUP — avision beyand — You howe fo rotate the Image in -place, which means__You _ You are given an 2%2 nxn 2D matic representing an Tmage _ | 19tats_ the image by 40 wteqree clock wise * tL have to modify the input 20 matrix directly _ —I __* Do not alldcate another 2D matix and “do the rotation 4 _ Example ‘- _ ‘Tnput > | 1] 2] 3 714 |! 4) S| ¢ > g Gg 12 i 7 B13 g 6 te | Tate transpose of the malin oe ee ee Tye} i Ae G| S{6 = Al ais 8 ai 7{[ 219 ai 61g a — 7 4] then — | reverse each Yow of fhe transpose matix _ Resultant matrix 714 |! g 2 3} 6]{3 @ © 3 j RAISONI GROUP — avision beyond — ee 4) tee eee oe oe eee ee ee ee i ee Oe 2 ee Merge. sorted Ay in ron space :- ou_ are given two ‘integer aways numi’ 4 ‘num2’ soyted g i, tn non- decreasing _oxder_ and_4wo integers _m $n. yepresenting the fumber of elements in oum1 4 num 2 tespectivel 4 . _ Merge num | 4 yum2 into a single anne Sorted in non - = détreasing order Example :-_ _ _ — alt= [1 j4}7 | & | 10 size=m CJ = |e | 3) 3 Size=n Approach a Take one_m a a of _size (m+n) - and then _ Q2 elements in the _ 93 atray a uy - sort it 7 7 - i _ __ Take out eke m clements from Gg put in a then take NM elements from ag put in a, oe 3 put in ay __ te: = O(nlogn) + on) + on) | ~ 0(n) a [sez oc) J wi: Tf) 4 7 € 10 oar2-> 2 3 Qq “Traverse ice’ | in awi 4 check if aml[i] > ame(ij v if yes then Ta) 7) afio swap low a1, 23(i1) 2\ 3 | g i‘]a| 7 ejio Te = O(nxm) s4° Sc < OG) Sort wt ita ls [aT 4 | Ul wer py | most- Optimized method :. ¢ Linked- List Cyd method e' 2345 6% € 9 EEtsisistetst Tt) oe =) 2 7 then we fike element preset at index > 2 aod \ = { 3 fast 2-393 qT 6 then take elumeut of 9 index “aw La! This process date: place G Ageuu 2-3 4 again dike wise Vo 15 2 a t 2—-9 Y t \ ae 3 cy sisted T “ Re ot / slow it moves 2 Step pointer > 't moves 2 Step \-—* S aa * } Ng a y foot 15 Now we will oFely et fas 3 / Now we will plas, tu fost poiuter a first fast 1S: a-2g', = Bes J Move sbv $ fast poiutey & J { *% 1 & or bow » 3 1 / Te = O(N); Sc= O11) RAISONI GROUP — avi sion beyond — Slow = nums[o] ; fost = nums[o]; _ while (slow |= fast) § slw = nums [slow] 5 | fost hums [nums (fost J) 3 4 Fast = nums{[pJ > while Cslow | iS fast) { Slow = _numsfslow] i (l fat = nums (feot]> j yetuin Slow: ee Tt OO Se edd et ts a —— Pi 4 , f Aka x }} RAISONI GROUP S 6 — Avision beyond — Approach 2 eee | Using Maths a aa dit the missing no. > £ lu ttpe aa fo. wa _ “$= Sumof. Noturaf no. from 1 ton — s= n(nt) ——@ 2 £2 Sen Squares of natural ne. _ 6 — 4+sum of — 2 Py “p= n(n4i) (anti) a — — — 3 s _ aig oe ame also calatak Sum n then Subtrace the Now Gives a — J x-Y= s! _ ee ee a =o ye a Cs) eee “IRE S s(n) xe-yr= PoP 77, a - | Ce4y} Gey) = _P Ge+y) est= pl cand find Y- = R+ty= SatebhbrZD :- () Tnversion of Array *- for a given integ ev aw size' n' Containing all dighnd voluzs , Find tu total no. oF Inachrgiona thot m exist ) 3 said to be a inversion when A poe Cow, ar [j a) law [iJ > aw (j) and \i< j Ex’ nums = $513,211, 4f There are 7 paw (50) 0913), (5,29, (5.4), (3,2) (3,1), Qi) #( for the eet which is Sotted in SEcrRasny ordey, the Moximum inversions = N*(n-1) ~~ > Approach Brut fore : Traverse thro ugh oriny with two loops and cheek i{ar(J > avGJ $3 39) Cout +t 3 TE = O(n2) $c.=0(1) © RAISONI GROUP — 2 vision beyond — 50 if will present in the viqia part (20 < target) a : doo igh 4 g (IS 7 mid= 8415 = 12 —3(56 )elewext 2 — ~ a - d Bt, 3 Sh present at (0) : 5 i : 4 = _ Pl But aur target elem eut — at Left net = >» tow Aigh . g ig 7 a mid= Stil = 19 ~(G)—> Cement = 30 7 a 2 2 » _ Matches with the target elem eut _ _ a oretum _Ftriuc- _ = > int n= Matrix .size0 5 TC = 0 (Uog(mxn)) | 2 int_m= modtvix[o]- sized); $.c= 60) 2 a 7 int low = 0 4 high = (mem) - 1 5 7 "a while (tow <= high)§ oe - = ; oo int pid’ = (tow + Chigh- tow) 2 —— i Caatrix [mid fm J] oid yom] = — _ Jou = micl +f Ue (abi) righ = mid -15 retumn folse; @® Paw (xin) int alalat, Given a double ' x! i ine NC _ Yaised fo power tae basically smplem pow Xe D Input» = 2.00000 = 0 Output = 1024.00000 double ans = 1.0 for (int T= 3 1403 [ +£) AMs=ans *K 3 3 pretuyn GNs > “Time Complex = O(n) S.c = O O) Optimized’ Approach *( Using Binary exponentiation | 4> = 44 Co = 167 (6? = (Wx 6)! = = 256) 6 asé'= 256 x @56) I = 254 Re Hi~E