Skip to main content

DSA Roadmap by HeyCoach

Page 1

30DAYS DSA

ROADMAP

Swi pef ormor e>


DAY1 Ar r a ys

Maj or i t yEl ement Di f f i cul t y-Eas y Gi v ena na r r a ynumsofs i z en,r et ur nt he ma j or i t yel ement .Thema j or i t yel ementi s t heel ementt ha ta ppea r smor et ha n[ n/2 ] t i mes .Y ouma ya s s umet ha tt hema j or i t y el ementa l wa y sex i s t si nt hea r r a y . Sol v et hepr obl em i nl i nea rt i mea ndi nO( 1 ) s pa ce.

Ex a mpl e1 : I nput :nums=[ 3 , 2, 3] Out put :3

Ex a mpl e2: I nput :nums=[ 2, 2, 1 , 1 , 1 , 2, 2] Out put :2


Maj or i t yEl ementI I Di f f i cul t y–Medi um Gi v ena ni nt egera r r a yofs i z en,f i nda l l el ement st ha ta ppea rmor et ha n[n/ 3] t i mes .Sol v et hepr obl em i nl i nea rt i mea nd i nO( 1 )s pa ce.

Ex a mpl e1 : I nput :nums=[ 3 , 2, 3] Out put :[ 3]

Ex a mpl e2: I nput :nums=[ 1 , 2] Out put :[ 1 , 2]


Fi ndt heDupl i cat eNumber Di f f i cul t y–Medi um Gi v ena na r r a yofi nt eger snumscont a i ni ngn+1 i nt eger swher eea chi nt egeri si nt her a nge[ 1 ,n] i ncl us i v e.Ther ei sonl yoner epea t ednumberi n nums ,r et ur nt hi sr epea t ednumber . Y oumus ts ol v et hepr obl em wi t houtmodi f yi ng t hea r r a ynumsa ndus esonl ycons t a ntext r a s pa ce.

Ex a mpl e: I nput :nums=[ 1 , 2, 3 , 4, 2] Out put :2


Fi r s tMi s s i ngPos i t i v e Di f f i cul t y–Har d Gi v ena nuns or t edi nt egera r r a ynums ,r et ur nt he s ma l l es tmi s s i ngpos i t i v ei nt eger .

Y oumus ti mpl ementa na l gor i t hm t ha tr unsi n O( n)t i mea ndus esO( 1 )a uxi l i a r ys pa ce.

Ex a mpl e1 : I nput :nums=[ 1 , 2, 0] Out put :3 Expl anat i on:Thenumber si nt her a nge[ 1 , 2]a r e a l li nt hea r r a y .

Ex a mpl e1 : I nput :nums=[ 3 , 4, 1 , 1 ] Out put :2


Turn static files into dynamic content formats.

Create a flipbook
DSA Roadmap by HeyCoach by HeyCoach - Issuu