0% found this document useful (0 votes)
42 views

Adobe Scan Apr 08, 2022

Here are the steps to solve this problem: 1) Input the building speeds A1, A2, ..., 2N of the 2N workers. 2) Sort the building speeds in ascending order. 3) Group the workers in pairs such that the first pair contains the workers with the minimum and second minimum building speeds, second pair contains workers with third and fourth minimum speeds and so on. 4) The building speed of each pair will be the minimum of the building speeds of the two workers in that pair. 5) The maximum sum of building speeds of the N associations will be the sum of first N minimum building speeds after sorting. So in summary, the steps are: 1)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Adobe Scan Apr 08, 2022

Here are the steps to solve this problem: 1) Input the building speeds A1, A2, ..., 2N of the 2N workers. 2) Sort the building speeds in ascending order. 3) Group the workers in pairs such that the first pair contains the workers with the minimum and second minimum building speeds, second pair contains workers with third and fourth minimum speeds and so on. 4) The building speed of each pair will be the minimum of the building speeds of the two workers in that pair. 5) The maximum sum of building speeds of the N associations will be the sum of first N minimum building speeds after sorting. So in summary, the steps are: 1)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Week-3

Lab Session:
Date ofthe Session: Time of the Session: to
Prerequisite:
Divide and Conquer Algorithm
Sorting
swapping items in place
Partitioning of the array.

Pre Lab Task


I. Trace out the output of the following using Merge sort. 10, 49, 32, 67, 45, 4, 7, 2, 1, 51, 78, 34, 89, 87, 36, 29,
3, 9, 11.

Solution:

o 3 Gs as3 r&7 36| 2


3
n-19 3

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/

lude <stdio h>

q soat (int [1 int int);

mdnt)

int n,',t:

Scant ("d", &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:

qsoA int a int,jnt lost)


nt i,temp, fivot

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

Power N distinct integers.


A having
narray as:
is defined
of the array
wer
max(A[il-AG]) where 2sisN
i such that A[j]<A[i|l.
index less than
is the largest
for each i.j {1,2,5},
then the power of the array is max((2-1),(5-
ther
2)), which simplifies
array
is
the
ay to 3.
which is equal
1.3)

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/

m includu < Stdio.h7

int mainl )

int ni
Saant("d ', kn);

forioien;i++)

Scan4.d",&aCi],
9uck 9,0,h-)

PintH sotel st ");

ori=0 i<n ;itt)


Foint4d ai]

uic int atj, int fixst, int dast

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/

Program Hnclude <stdio.h

mogesot int
,in+
Void

sgeauajint int, int n t ):


vod me
int

void main )

int , n
Scant"7d" ,&n);

toa i-o, i<n;i++)


Scant7d", kan[i]

moge soyt (o,n-);


Psinttl Soated data ");
for=0 fen ,i t7)

Pintfl"1 ", avCJ):

Vod ms qe sotint A, inth)


int m',
42
(sh)
m-t

meogeso4 (J, m)

mosgesott [h,mt 1);


hegtao (4,m, m41, h,

mog A,m,mt, h)

Void merge o a int a, int b, i t Cin t A


ind t ltco

tnt 0, : = o

ohiocohilo (ie = b&s j<=d)


(aiJ< av)

+k4)= auljtr]
whisie =b
4[ aulia t]
Lohil (i rd)

folio, jro, id,


ditttt
itt
jtr
Inrut
af1A
32R
Outfut
3
21SC1202 - DATA STRUCTURES

Post Lab Task:


Gifts:
1. BennyandAac iust taken
taken a shower. Now she is going to buy some gifts for her relatives. But the problem is
a
hasjust
Benny know how to reach to the gift shop. Her friend Mike has created a special set of instructions
Little pig know how to
reac.
doesn't
thatBenny is a string which
consists of letters {'L, 'R', 'U', 'D'}.

farher A set of instructions


is staying at point (0, 0) on the infinite plane. She consistent lh 6.lG
Ils all
assume that Benny
Eorsimplicity, let's Mike. Lers assume that now she is staying at point (X, Y). Then dependina on

theinstructions written by moves in s o m e direction:


instruction she
thecurrent
(X. Y) moves to point (X, Y 1) -

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/

Program #nclucu< stdigh>

int mainl)

i0h,1
Scantl'rd" an)
fori=oien,i+*)
foant|d", saCi1)
qudk lO.n-)

tor o icn jit4

pind a:1);

uick Int aC7 Int fvst, int oit)


int
Hl4<)

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

Program +Includt <stdio hnz

void quicksort int aInt fisst, it last)

int i,i, Heme, Pivot


H Arst<lact)
pivot = H:st;

ist
last
cohi& C1i)
Cwh&(ai]< = alrvot7)

+t

cwhle (ati] >a [rivot]

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/

Program includ<stdio .hz

int mcinC)

Sant/A", k4);

whillt-
int n,S)

SCantzd 7.d", kn,ds

int alnJ

4 Cint 1=0, i<n,i44)

Sant" d", &alij]);

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 /

Program include <stdio.h>

int main( )

n4 trm
Scant.d", +),

cohi&(t--)
scant7.cl ", a n);

int min = loCa, max= -lo C a,

for int i=0, i


<nit4)

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/

tinclud < Stdio.h7


int maint)

int tn

Scant "1.d', Lt);


whtt--)
Scant " ",án);

int minlo ea maa-loea, a,

oy (int =0, 1<n i4T)

Cand"7d",ka )
a > maa)
max a,

se a<min)
min

Poin J", mint maz);

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

saan d d , &h, 4 i); gram

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

You might also like