Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
44 views15 pages

Prefix Sum - Notes-2

The document discusses calculating prefix sums for 1D and 2D arrays. It provides examples of calculating prefix sums to find the sum of elements in a given range of indices for both 1D and 2D arrays. It also discusses an optimal approach using prefix sums to solve submatrix sum queries in O(1) time.

Uploaded by

Shivam Rai
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)
44 views15 pages

Prefix Sum - Notes-2

The document discusses calculating prefix sums for 1D and 2D arrays. It provides examples of calculating prefix sums to find the sum of elements in a given range of indices for both 1D and 2D arrays. It also discusses an optimal approach using prefix sums to solve submatrix sum queries in O(1) time.

Uploaded by

Shivam Rai
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/ 15

Today's agenda

PoefinSum on ID Array
Prefin Sum on 2D Array
11Array recap

int as 153 Lio 25 35 To 503


Point aorta 30
I Prefix
array
Given assai return PLED where
i Sum asolo E aioli all
Pf as
for
En arts 15 i 583
Pf153 25 7 14 11193

assess 3824558.53
s L 33 5 9 14 16 24 15183
Pf

I Bouta
force
arts 15 5 i 383
Pf 153 95 7 14
at 3
into Befin int assn
int
Pfs
offs ascot i s m

int Sum o 2 Sm O
g
p SEE
for int
j o
ja ja
Sumt as Cj
3
571 3
3
3 G Pf fi sum

To 062
I optimal approach

assist 15 5 i 383
Pf153 95 7 14 3

Pf 2 SumCo 2 Sum o 1 A arok 14

Pf Eje

4
Pf133 Sum 3
o Sum o 2 A aso 3 I 14 3 11

I
Pfaff
n
I

I a Pfli D
Pf fi i
Sum o t i Sum o I 1 A
anti
I

Pffi PfSi e tasoli

arts 15 5 i 383
Pf15 95 7 14 11 193
I PSuedo Code

into Befin int asolo


integers
asolo
Pflo
I Olad int it ian itt h
for
Pf fi Pf fi a anti

y return Pf
a Given a elements and Q queries
assay for each query
calculate sum all elements in given range
of
En an Ceo L 2 I 3 3635393
0 6 11Brute
force
K R ans

4 8 14 void SumQuery int arlad into queries


3 7 15
1 is itt I
3 15
for int i o a
0 4 int c queriesfi o intr queries

6 g int Sum o

7 7
for line j L jar jtt h
OCEAN
Tc Sum Sum an Cj

3
Point Sum
3
I optimal approach

aorta L 2533635393
Io 2 5 8 13 19 22312327293
0 6 Pf
K R ans

4 8
3 7
Sum 4,8 Pf183 PfE3
stem o 8
Stumm
6,3
1 3

Sum 3,7
Pfa Pfk
Stum1927
6 9 satico
7 7

Sum Lir PfEr Pfc a


stmfar StmCo La

Sum6,4
Pfa
Sutley
PII
too
I PSuedo Code

void sumQuery int arlad into queries


Cass
int
Pfl Bef in optimal

Fe OHA
is it 71
for int i o Q Oca
int c queriesE lo intr queries eolata
if i ok
int ans
Point ans
PIER Pfc 13

else d
int ans CRJ
Pf
a
g point ans

brute force optimal approach


t I
Can O Cath
Q2 M
I t
0 m OCHEOLA
Representing Submatoinn
O 1 2 3 Y
O CTD ER
a C ED
e
2 I l gg
3
a Cn Es
5 CBI CBR
6 5 I I Iz
To represent a matin

2 her Points
TL BR
Q Given a matter and Q queries
for every
Submmatoinsum
query find O 1 2 3 Y
o 7 I 6 3 13

Q I 10 5 I 0 9
6 4 3 8 11
TL BR 2
13 8 5 124
2,1 4,3 203
3 2 1 9 8
32 5,4 384
j 4 3 2 6 5 6 5
Cara

Boule force
iterate on the Submatoin
for every
the
query
and get Sum

Tc O Q Nara

I optimal approach

PSumSiJiSumCoi PsumfiJfjJeSumC

o Ci.j
PsumfiJljJeSumC o Ci.j

o 1 2 34 Pf O 1 2 34
o 7 I 6 3 13 o

I 10 5 I 0 9 a Ig
2 6 4 3 8 11 2 33
3 13 8 5 124 3 423
4 3 2 1 9 8 a

j 4 3 2 6 5 6 5 6 5
Cara Cara

Pf12313
3333

Pf13133 19

Pf 3312 23

1120 Poefin Sum


AT 0 I 2
0 a b c a 9th atone
Apply sow wise
t d e
f sum d ate dtetf
h i Befin gthti
2
g g gth
wise
Apply Col
Pf 0
a 2 Poefinsum
o a ats ft
atb's
a atd g fatty a atb
29dg Getting't
p atdq.be'd Eastep
gthti atstedt atstadtetf
atdtg etgth tgthti
I Query solve

t
o 1 2 34 Q

cat cat
2 GO Br

PSum 4313 Psum 1313 Psuma


I t
Sum eo.o 437 sum eo 375
t Psum E So

22
in 64,4
Tc BR

5
Pf 434 PLATED PLATE
sum 074,47 Pff3CD
Pf
yr 72 Query

its
EL 2
ya

PsumED A DumCal 1 Cyz


Puma 2 Cy 1 t PSumal 1 Cyr is
I Psuedo code

submatoinsum int JE mat int cos queries

int CIO Pam mat Hw


June

int i iequerieslength itt L


for O

int ni queries i Co
int gi queriesE lol
int 22 queries i Co
int ya queries i lol
int seam O

sum PSum1223923

if 21203
Sum Psumal 13927
I of
iffy
sum Psum 22391 1
3
217022 yi o s
if PSum121
J
sum t 1381 a
J
3

7C Olam t o Q a o Caan to
o d 2 By Query

91114
2
I yi
in 92721
1

3
y PSumf2 A Dum 21 1792
j
o Psama2 Cy 1 t PSumal 1 Cyr is

Psum 5 Ca PSE I

PEACH PSYCHED

You might also like