Today's agenda
Poefin Sum
Sum in a
range formultiple query
Equilibrium in der
Product
of array except itself
number
of odd numbers in givenrange ETI
Rain water trapping Hardteletcode
a Given a
array
elements return
Pff where Pff Suharto
anti
assn
for all I
En assist T I i iz is
s 4 5 11 9 163
Pf
asscio L i 5 I 3 4 19
Go h 2 3 4 7 11 12 191113133
Pf
11 Brute force
h i loop
for every index from 0th
sun a to
ith inden and add all the elements
assist T I i 3 is
Pf 15 14 5 11 9 63
Haneda code
into Prefinsum int array
int
Pfa
int i O ian itt l
for
int Sum o
for int j o
jai jtt l
SC Oct Sum Sunt asolj
gum
ppg
return
Pf
sit
Sum O 47176
assist i i i
Pf15 4 5 11
I optimal approach
axon
asolo
Pf lo
Pff asolo asf pffo tarot
Pdf
Pf 23 asolo and 123 tarot
t an
Pfa
Upfa
asolo tanh arfa tarts
Pf 3 2
Pf Pf 2 taxis
assist i i i i it
14 5 9 163
Pfg 11
Pfli pffi Dt anti for all i
IIPuedo Code
int 13 Befinoptimal intarsia
int
Pfa
asolo
Pffo
Tc Oca int i ica itt
SC Oct for
Pffi Pffi Ba a solid
return
Pf
3
a Given a assay elements and Q queries on an array for
each query calculate Sum all elements in given range
of
En and 2 5 I 3 i i i i s
Q 5 had Bouteforce
to 3 8 55 Literate in the range for
12 4 8 every query
2 o 3
3 f g void SunQuay int arm int aeries a D
g g g
int i O is a it K
for int L Queries fi fo
int R Queries i a
T C O Q N int Sum O
Oct I
forSum Suma jer jtt
int
S c
j L
asolj
g
Point sum
y
10Ptimal approach
and 25 I 3 i 828
2 3 4 7 11 12 19 11 13 13
Pf 10
Sum 3,8 Pff Pf 2
stulo8 SamCo 2
Sum 2,4 Pf14 Pff
Stanco4 Shimla I
Sum o 3
Pff
Simcoe
Sum L R Pfer Pfa I idk
a o
Saffo R stunCo L 1
Sum o R PIER
stumlo D
ifl o
I Psuedo code
void SunQuay intarsia int aeries a 1234
int
PfCJ Prefinoptimal arr
lint i is a it K
for
O
int L Queries1703
int R Queries i a
T C o a HOLD h
TO Cata if c o
s c old point Pf r
Pfk 13
3
else f point
Pfr 3
3 3
ate Olata
N Q
0 N O Nta o 2nd IO n
p c
a Equilibrium Index
Given a
assay elements Count no
of equilibria
index An inden i is said to be equilibrium inden if
Sum all the elements Sum all the elements
of before of
ith index ith inden
Sumfo i t
after
Sum itt n t
En a fu I 6 3 4
leftsum O ans a
sightsum 13 7 4
area i s i i 5
leftsum sans 3
sightsum
9
11optimal approach
45
a fu 2 6 3
4
Pfa 2 7 11
Sum i t i t
leftsum o
Pf
sightsum Sum ite n o
PfCn D pf it
Pfa 1 Pfli
I Puedo Code
int Pirotionlint array
int
Pff Prefin as
int ans o
Lint it is a a it k
forint
Pf i i
leftsum
int oightsum PfCn D Pf i
T.ci Ola told Ceeftsums sightsum fans tf
2nd IO a
if
SC Ocn 3
if o
Pf In D Pf o 9
ans t t
g
11last inden Check
PfCn 2 o ans tt 3
if
return ans
a Product Encept itself
of Array
Given area return an answer
array
Such that answer fi is equal to the Product all the
of
elements except numsfi
of mums
note
you can't use division operation
En any L I 3 I
ans 4 24 12 8 6
PsefinPod i asolo and a solid
any L I 3 I
Poprad 14,4288443
Suffin 1 4 4 3
1222
24
PoefPrdfi PofPrdCi D Saffir
PrfPods PofPrd 2 1
PofPrd127 PofPrd 4
PofPrd f 3 PofPaolo 12
I Psuedo Code
into poodwetenceptitself int arose L
int PofProductIn
PofProduct o asolo
int it ice itt l
for
Pofpooductli PofProductCi D ascoli
Tc OCan
s.cc o int 1
Suffin
Lint i n e i i K
for
o
PofProduct
fi PofProductCi D Saffin
Suffin Suffin anti
3
PofProductCo Suffin
return Poffooduct
3
Tracing
int PrfProducts Goofy L I 3 I
PofProduct o asolo
PofProduct11
for int it ice itt l 44 428 443
Pofpooductli PofProductCi D ascoli
Suffin 1 4 4 3 12 2 24
int Suffin t
int i n e i i K
for
o
PofProducts PofProductCi D Saffin
Suffin Suffin anti
PofProductCo Suffin
11Poefinman and Suffinman
psefinman
En a 623 I I 3 2 I 5 i 32 I j j
Pmano 2 2 3 3 3 3 4 4 4 4 4 4
Prefinmanli man o i
suffirmer
Suffinmanli man Li n t
a 423 i i 33 is i
Samanta 4 4 4 4 4 4 4 33 3 3 1
a Rain water trapping
Given a non negative integers representing an elevation
map where the width is
of each bar 1 Compute how much
water it can toad
after raining
En assfiz 2 1 3 21 2 4 32 1 3 1
ans 8
O
is 11111111 ooo i 1111
2
3 2
2 2 2
o I 2 3 4 5 6 7 8 9 10 11
Calculate amount at each wall and add them
I
ans
En
3
2 2 2
1
A
lb 2 3 lb 2 Jb min45,257 2
Yb
2b 2 amount 2 a a 66 3 amount 2 1 1
3 3 4
2 3
2 2
y I
of i
86 3 mindsog 3 lb 3 minds b 3
Jb Lb
ly 3 amount 3 1 2 y y Amount 3 1 2
5 4
3
lb man height on left 5 min b B I8,6 5
b Yb
8 man height on sight
5 1 24
Amount
1111111111 ESET'd 1111
0 I 2 3 4 5
64
7 8 9 10 11
e a 2 3 u g É 7 8 9 10 is
ANC 2 1 3 21 2 4 3 2 1 3 1
PmanC 2 2 33 3 3 4 4 44 4 4
Smart 4 4 44 4 4 4 33 3 3 1
lb Pman.fi 1 3 Yb min 3,67 3
B Sman fit Conto 3 2 1
Amount 041 140 1 1 2 224 1 5
I Psuedo code
int rainwater int arm I
flew
int Pman I as
funct
int Sma C ar
funez
int amount og
isnt h
forcintist 1 itt
Sec 012m0cal int lb Pman fi 17
t
011 int ab Sman ite
int min Ceb ob
Two inters
yb
int contai anti
yb
anemones
gang
3
return amount
into
funds int arm I
int I Sman new int N
s man n 1 an In 17
int i i i D1
for n 2 o
Sman fi Math man smanfitt as i3
return Sman