Adobe Scan Apr 08, 2022
Adobe Scan Apr 08, 2022
Lab Session:
Date ofthe Session: Time of the Session: to
Prerequisite:
Divide and Conquer Algorithm
Sorting
swapping items in place
Partitioning of the array.
Solution:
m 0tl
2 13 14
m 0t9
n- 10tl= 1
5 G
m 12 13
t 2
m
1'AITIAN
2
38
1
solut
T 2
O 61
to 12 3 G17
32 su 57 su
4S s 67 7
2
21SC12002 DATA STRUCTURES
Trace the insertion sort algorithm to arange the following data in ascending order 49, 32, 67, 45, 4, 2. 1.78
, 89, 87.
plution:-
49 32 5 4
18 348 87
32 4967 5
8 348 87
No Suwap equired
39 49 G7 45 422 1 18 34 89
32
3 q 45 G1 4 2 834 &9 7
32 45 T 21 18 34 9 &7
32
3a 45 4 4 67
34 &7
32 5
7 8 34 q 1
32 4S 78 34
32 15 4
18 34 8987
32 5
G 18 34 8987
32 35
G7 S7
8 34 8 7
32 35 78 34
3 35 18 34
39
32 H5
3H &
&
3 45 67 & 34
2 3 5 G 18 34
&
32 5 a
118 34 8 7
SwgP
32 5
67 18 34
4 3a H5 H 6 34
&87
32 H5 H 3H G7
2 3
x 34 H5 G7 r87
No SwaP
2 H 32 34 H5
G7 &97
2 3 3 HS & 7
atter sorfing
32 3H 45
S 8
21SC1202 - DATASTRUCTURES
array AA of size NN. Sort the array using Quick Sort and print the sorted array.
lechef.com/DSCA2019/problems/NSECDS06/
mdnt)
int n,',t:
whilu (t-)
Scan"/.d", kn)s
fT0: ien;it*)
sCanf 1d,&ati1
SoH (,on-0;
oa (i=o:ien; ia+}
int (4.cd", ali]);
etuin 0:
4(rstclast) 40
Pivot-Rrst:
-hrst;
j last
uhie(i i)
Cwhil (je - afeirot J & ielast
+t
cwhie(aUJ>a[rivot3
Rli<i)
ail: atJ:
aiJ= teme
tema[ivotJ
afeivt a[i]:
a] temf
sort (, rsj.
sort aj+l, last);
nPut
out Put
-3 O
1
30 23 55
5 32 o
21SC1202 DATASTRUCTURES
tion Allowed:
and y and swap A[x] and A[y], find out the maximum power that
choose wo indices
any two
x
allowed to
re
/www.hackerearth.com/practice/algorithms/sorting/quick-sort/practice-
achieved.
ms/algorithm/increasing-subsequence-fbb63e3c/
int mainl )
int ni
Saant("d ', kn);
forioien;i++)
Scan4.d",&aCi],
9uck 9,0,h-)
nt P,i,, J:
Pirst
ist 41
lat
guicksost (a,i-1, Asst);
quickot a)t, last);
înt main )
int n, Pivot
Soant.d kn)
inH an,ij
forli=0,ien; it +
Ccandl"/d", kati);
uiCo(,0, n-J
for (i-0, ien
,i+1)
Printld", atij
Inut
3 0
OUtPut
OH 7
O
21SC1202 - DATA STRUCTURES
Sumf of
Building Speedyou
3.Maximum
the king of
Pennsville where
Penn
have 2N workers. All workers will be grouped in association of
You are the N associations have to be formed. The building speed of the ith worker is Ai. To make an
associations
total of N
size2,so
a nick up 22 worker
workers. Let the minimum building speed between both workers be x, then the
you pick up
association, building speed x.
resultant
has the
association
of the of
print the maximum value possible sum
building specds of N associations if you make the
You have to
associations optimally.
https://www.hackerearth.compractice/algorithms/sorting/merge-sort/practice-
problems/algorithm/maximum-sum-of-building-speed-00ab8996/
mogesot int
,in+
Void
void main )
int , n
Scant"7d" ,&n);
meogeso4 (J, m)
mog A,m,mt, h)
tnt 0, : = o
+k4)= auljtr]
whisie =b
4[ aulia t]
Lohil (i rd)
L'- from
to point (X. Y + 1
R - from (X, Y) moves
to point (X 1, Y)
-
from (X. Y)
moves
U-
to point (X + 1, Y)
D - from (X. Y) moves
winter now. initially, all points are snowy. But if Benny have already visited
The weather is cold because its
becomes icy (because Benny has just taken a shower). Every time, when Benny
some point at any time this point
she slips and falls down.
a step into icy points
makes
which denotes a set of instructions for Benny. Your task is to calculate how may times
You are given a string S
down.
shewill fall
https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/practice-problems/algorithm/benny-
and-gifts-marcheasy-3/
int mainl)
i0h,1
Scantl'rd" an)
fori=oien,i+*)
foant|d", saCi1)
qudk lO.n-)
pind a:1);
cohie lij)
Cohilo
[)afra)
whiCa]> atr
Cali7,
ai1-a[iJ
ailC
Cail1
a[iJ-a(r7
aCe C;
GuicFat (9,4, J)
eut
ReULA
Out Put
21SC1202 - DATA STRUCTURES
The Meteorites
2.Earth andme the Earth was a flat rectangular landmass. And there was no life. It was then that the sky lit up
Once upou a l l i n g from out of space. Wherever they fell on the planet, a river was born, which flowed in all
with metecorth, East. West, South), til the waters reached the cdge of the Earth and simply fell off into
space.
and divided the one huge landmass (Pangaea) into
many smaller landmasses.
Now t these s rivers criss-crossed
Now, Gthere was no life. remember?), want to know the number of landmasses on the planet after all
h a v e fallen. They also want to know the area of the smallest and largest landmass. Can you help
hel
this question?
the lifeless in
h t t p s : / w w w . h a c k e r e a r t h . c o m / p r a c t i c e / a l g o r i t h m s / s o r t i n g / q u i c k - s o r t / p r a c t i c e -
p r o b l e m s / a l g o r i t h m / e a r t h a n d t h e m e t e o r i t e s - q u a l i f i e r 2
ist
last
cohi& C1i)
Cwh&(ai]< = alrvot7)
+t
3++
4(i<i)
temfa[ 3;
aC:3 a1:
oC: tm
44
Heme- a ] ;
ai a[rivet J;
a[PivotJ kmP
9uicot(a, j-1uhas):
quicko a,j4', last):
int main()
int h, Pivof
Scan!("d", n);
int an],13
for (i0 i<n; itt)
Scant.d'", å at iJ)
quicoat(,0,n -
for -o,
ien, 1+*)
Pintt"d", ati1);
APut
Output
3
5
Skill Session:
1.Sam Height <HSBC>
friends wants to go to watch a movie in Armstord Cin
Sam among his ecial about Armstord cinema whenever people come in the
There is something group here. They will
eiohts, Sam as a curious guy alvays wants to sit in the middle as
cinema has the best
get seats
est vie
view
from the middle.
leader of his group decides who will join him for the movie
Now. Sam as the
N-1 friends with him (N including him).
Initially, he has the heights of Sam's friends
N-1 numbers that represent
You are given
Sam well.
the height of
as
You are given
two operations:
Now, Sam can do
new friend
of height H.
1. He can call a
of his friend invitations.
2. He can cancel any
httos://www.hackerearth.com/practice/algorithms/sorting/merge-sort/practice-problems/algorith m/alice-
and-marks-hsbc-b18d5d01/
int mcinC)
Sant/A", k4);
whillt-
int n,S)
int alnJ
Pyintt "n");
in + i-0
1<s0
else
45
For int i<o1n;id*||
4(srafi
Ct
(Se ali)
Ctt
e=-2c)
Psint#"n");
nc> E)
Piint4(d, de -nc);
Tctuin 0
3
ineut outfut
5
21SC1202 - DATA STRUCTURES
2.
YouMedian
are given an arTay A of N integers. You perform this operation N-2 .times: For each contiguous sub array
h a than
size greater
n 2,2. you find the median of each sub array (Say medians obtained in a move are
of odd
M3,..., Mk). In each move, you remove the first occurrence of value min (M1, M2, M3,.., Mk) from M1, M2.
the
orioinal aray. After removing the element the array size reduces by 1 and no void spaces are left. For example. if
element 2 from
the array {1,2, 3, 4}, the new array will be {1, 3, 4}.
wer e m
Print
ove
a single integer denoting the sum of umbers that are left in the array after performing the operations. You
test cases.
this for T
need to do
https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/practice-
p r o b l e m s / a l g o r i t h m m e d i a n - g a m e - j u n e - e a s y - 1 9 - 3 7 2 2 b e 6 0 /
int main( )
n4 trm
Scant.d", +),
cohi&(t--)
scant7.cl ", a n);
San("/d', &a);
i a max)
max=a
el 1f a<min)
mn=a,
Psint4"7d", min
+ma x),
3
46
Outrut
neut
5 3
21SC1202 -
DATA STRUCTURES
Neighbours
nnty
n- mp sets of positive integers SI,S2,...,Sn. The i-th person can live in
house from the
any
have to choose a house for
each person. More formally,
you have
to create an
An Such that for all i, AiESi and Ai denotes the house of the i-th person. Let Bi denote
the
een i-th person and the closest neighbor to his left (some person j#i such that Aj<Ai and Aj is
he doesn't have any such neighbor, we say that Bi=0. Let Ci equivalently denote the distance to the
or to his right.
e to create Al,A2,...,An in such a way that 2B+C is minimized.
the minimum possible value of 2B+2C
hackerearth.com/practice/algorithms/sorting/merge-sort/practice-
orithm/choose-one-c4672347/
int tn
Cand"7d",ka )
a > maa)
max a,
se a<min)
min
47
nput
3139
2
225
Out rut'
21SC1202 - DATA STRUCTURES
4. Different queries
You are given an aTay A of size N. You have to pertormQ queries on the array. Each of these queries belongs to
types of queries:
thefollowing
elements in the range [L,R1
1. LRX Add X to all
elements in
2 LRX: Set the value of all the range [L,R] toX
However, it is not mandatory to pertorm the queries in order. Your task is to determine the lexicographicaliv
that can be obtained by performing all the Q queries.
largest array
https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/practice-
problems/algorithm/jumbled-queries-afb23321/
Program
ncdude <stdio.h
Vod Sost (tong , Jongh,ong at1, Jong al1, Jong b[j, Jong
CLI)
(l<h)
ona m-4h)
Sort ,m, , ,b, b, ,C,C)
SoTt m+!,h, a, G, ,b,b, C, ,C);
me e1,m, h,a, 9, b,b,,
C,,);
voi
ng , Jong h, long a[, 2ong at7, Song pt 7, Jorg cl1)
= =mt\, k=\
cohilak=h)
1 m a& ic=h)
rlccrj< cO)
48
C.J= c Ci1:
ktt
5. L
alone
inks tl
n swa
int main )
S://w
P-507
eng afnJ
Sons A
s], isls, : ) ,ala]J,
,TT(2], [3], erl2, me-(ntJ:
Infut
to(o, i<nit)
53
sCand (d 7d ", tat1); 3 HS
si0,ikq 144) 3 4
Scan 7d" katil); 3
to o ; i<qit+ S
OutPUt
Scane"d rd d",
i (=1)
to,eiGJ; 3 5
SCand( d d
, &,y
n[cJ X
plc-z
Ctt