SlideShare a Scribd company logo
Prefix sum
It is a simple yet powerful technique that allows to perform fast calculation on the sum
of elements in a given range (called contiguous segments of array).
Prefix sum
It is a simple yet powerful technique that allows to perform fast calculation on the sum
of elements in a given range (called contiguous segments of array).
6 3 -2 4 -1 0 -5
Example -
A[ ] =
Prefix sum
It is a simple yet powerful technique that allows to perform fast calculation on the sum
of elements in a given range (called contiguous segments of array).
6 3 -2 4 -1 0 -5
Example - i = 0 1 2 3 4 5 6
A[ ] =
Prefix sum
It is a simple yet powerful technique that allows to perform fast calculation on the sum
of elements in a given range (called contiguous segments of array).
6 3 -2 4 -1 0 -5
Example - i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5Prefix Sum Array -
A[ ] =
A[ ] =
Prefix sum
6 3 -2 4 -1 0 -5
0 1 2 3 4 5 6
6 9 -2 4 -1 0 -5
6 9 7 4 -1 0 -5
6 9 7 11 -1 0 -5
6 9 7 11 10 0 -5
6 9 7 11 10 10 5
6 9 7 11 10 10 -5
Example -
i =
A[ ] =
Prefix sum
6 3 -2 4 -1 0 -5
0 1 2 3 4 5 6
6 9 -2 4 -1 0 -5
6 9 7 4 -1 0 -5
6 9 7 11 -1 0 -5
6 9 7 11 10 0 -5
6 9 7 11 10 10 5
6 9 7 11 10 10 -5
for( int i=1 ; i<7 ; i++ ){
A[i] = A[i]+A[i-1];
}
n
Example -
i =
A[ ] =
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
Ans : 10 (=A[4]) O(1)
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
Ans : 10 (=A[4]) O(1)
Calculate the sum between range [0, 6] ?
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
Ans : 10 (=A[4]) O(1)
Calculate the sum between range [0, 6] ?
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
Ans : 10 (=A[4]) O(1)
Calculate the sum between range [0, 6] ?
Ans : 5 (=A[6]) O(1)
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
Ans : 10 (=A[4]) O(1)
Calculate the sum between range [0, 6] ?
Ans : 5 (=A[6]) O(1)
Calculate the sum between range [2, 6] ?
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
Ans : 10 (=A[4]) O(1)
Calculate the sum between range [0, 6] ?
Ans : 5 (=A[6]) O(1)
Calculate the sum between range [2, 6] ?
sum between range [0, 6]
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
Ans : 10 (=A[4]) O(1)
Calculate the sum between range [0, 6] ?
Ans : 5 (=A[6]) O(1)
Calculate the sum between range [2, 6] ?
sum between range [0, 6]
sum between range [0, 6] =
sum between range [2, 6]
sum between range [0, 1] +
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
Ans : 10 (=A[4]) O(1)
Calculate the sum between range [0, 6] ?
Ans : 5 (=A[6]) O(1)
Calculate the sum between range [2, 6] ?
sum between range [0, 6]
sum between range [0, 6] =
sum between range [2, 6]
sum between range [0, 1] +
A[6] = A[1] + sum between range [2, 6]
A[6] - A[1] = sum between range [2, 6]
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
Ans : 10 (=A[4]) O(1)
Calculate the sum between range [0, 6] ?
Ans : 5 (=A[6]) O(1)
Calculate the sum between range [2, 6] ?
sum between range [0, 6]
sum between range [0, 6] =
sum between range [2, 6]
sum between range [0, 1] +
A[6] = A[1] + sum between range [2, 6]
A[6] - A[1] = sum between range [2, 6]
sum between range [2, 6] = A[6] - A[1]
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Example -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
Calculate the sum between range [0, 4] ?
A[ ] =
A[ ] =
Ans : 10 (=A[4]) O(1)
Calculate the sum between range [0, 6] ?
Ans : 5 (=A[6]) O(1)
Calculate the sum between range [2, 6] ?
sum between range [0, 6]
sum between range [0, 6] =
sum between range [2, 6]
sum between range [0, 1] +
A[6] = A[1] + sum between range [2, 6]
A[6] - A[1] = sum between range [2, 6]
sum between range [2, 6] = A[6] - A[1]
sum between range [2, 6] = 5 - 9
sum between range [2, 6] = - 4
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Generalization -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
Prefix sum
6 3 -2 4 -1 0 -5
Generalization -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
A[2, 6]
Prefix sum
6 3 -2 4 -1 0 -5
Generalization -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
A[6]
A[2, 6]
Prefix sum
6 3 -2 4 -1 0 -5
Generalization -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
A[1]
A[6]
A[1]
A[2, 6]
Prefix sum
6 3 -2 4 -1 0 -5
Generalization -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
A[2, 6] = A[6] - A[1]
A[6]
A[1]
A[2, 6]
Prefix sum
6 3 -2 4 -1 0 -5
Generalization -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
To calculate the sum between range [I, j]
A[2, 6] = A[6] - A[1]
A[6]
A[1]
A[2, 6]
Prefix sum
6 3 -2 4 -1 0 -5
Generalization -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
Formula -
To calculate the sum between range [I, j]
A[2, 6] = A[6] - A[1]
A[i, j] = A[j] - A[i - 1]
A[6]
A[1]
A[2, 6]
Prefix sum
6 3 -2 4 -1 0 -5
Generalization -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
Formula -
To calculate the sum between range [I, j]
A[2, 6] = A[6] - A[1]
Calculate the sum between range [3, 5] ?
A[i, j] = A[j] - A[i - 1]
A[6]
A[1]
A[2, 6]
Prefix sum
6 3 -2 4 -1 0 -5
Generalization -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
Formula -
To calculate the sum between range [I, j]
A[2, 6] = A[6] - A[1]
Calculate the sum between range [3, 5] ?
A[3, 5] = A[5] - A[3 - 1]
A[3, 5] = A[5] - A[2]
A[3, 5] = 10 - 7
A[3, 5] = 3
A[i, j] = A[j] - A[i - 1]
A[6]
A[1]
A[2, 6]
Prefix sum
6 3 -2 4 -1 0 -5
Generalization -
i = 0 1 2 3 4 5 6
6 9 7 11 10 10 5
Prefix Sum Array -
A[ ] =
A[ ] =
i = 0 1 2 3 4 5 6
A[6]
A[1]
A[2, 6]
Formula -
To calculate the sum between range [I, j]
A[2, 6] = A[6] - A[1]
Calculate the sum between range [3, 5] ?
A[3, 5] = A[5] - A[3 - 1]
A[3, 5] = A[5] - A[2]
A[3, 5] = 10 - 7
A[3, 5] = 3
O(1)
A[i, j] = A[j] - A[i - 1]
• To calculate prefix sum array of n size array
Time complexity - O(n)
Analysis of Algorithm -
• Time taken to perform range sum query is -
Time complexity - O(1)
Analysis of Algorithm -
• Total time taken to pre process the n size array and to perform range query is -
Time complexity - + O(1)O(n)
~ O(n)
• To calculate prefix sum array of n size array
Time complexity - O(n)
• Time taken to perform range sum query is -
Time complexity - O(1)
• It takes O(n) time to calculate prefix sum array of n size array.
Key takeaway from this lesson -
• It takes O(1) time to perform range sum query on n size array.
Here is the link to video tutorial :-
https://youtu.be/pVS3yhlzrlQ

More Related Content

Similar to Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2

Patterns in Series
Patterns in SeriesPatterns in Series
Patterns in Series
Free Math Powerpoints
 
Numeros reales, inecuaciones y desigualdades
Numeros reales, inecuaciones y desigualdadesNumeros reales, inecuaciones y desigualdades
Numeros reales, inecuaciones y desigualdades
DanielaAngulo25
 
1Q LP 3.4 Venn Diagram & Set Operation (Complement and DIfference).ppsx
1Q LP 3.4  Venn Diagram & Set Operation (Complement and DIfference).ppsx1Q LP 3.4  Venn Diagram & Set Operation (Complement and DIfference).ppsx
1Q LP 3.4 Venn Diagram & Set Operation (Complement and DIfference).ppsx
MariaRaffaellaMargau
 
PO_groupVI_Term assignment.pptx
PO_groupVI_Term assignment.pptxPO_groupVI_Term assignment.pptx
PO_groupVI_Term assignment.pptx
PATELPUNIT2
 
Arithmetic sequence
Arithmetic sequenceArithmetic sequence
Arithmetic sequence
Cajidiocan National High School
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
MithuBose3
 
math 10 aug. 6, 2023.pptx
math 10 aug. 6, 2023.pptxmath 10 aug. 6, 2023.pptx
math 10 aug. 6, 2023.pptx
CheyeneReliGlore
 
Correct sorting with Frama-C
Correct sorting with Frama-CCorrect sorting with Frama-C
Correct sorting with Frama-C
Ulisses Costa
 
Estructura Discreta I
Estructura Discreta IEstructura Discreta I
Estructura Discreta I
U.F.T Fermin Toro
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
Unit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptxUnit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptx
G2018ChoudhariNikita
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
rowntu
 
Diploma_Semester-II_Advanced Mathematics_Definite Integration
Diploma_Semester-II_Advanced Mathematics_Definite IntegrationDiploma_Semester-II_Advanced Mathematics_Definite Integration
Diploma_Semester-II_Advanced Mathematics_Definite Integration
Rai University
 
Integers
IntegersIntegers
Integers
MartinGeraldine
 
Integers
IntegersIntegers
Integers
MartinGeraldine
 
Part 1 sequence and arithmetic progression
Part 1 sequence and arithmetic progressionPart 1 sequence and arithmetic progression
Part 1 sequence and arithmetic progression
Satish Pandit
 
Lec38
Lec38Lec38
On the Construction of Cantor like Sets
On the Construction of Cantor like SetsOn the Construction of Cantor like Sets
On the Construction of Cantor like Sets
theijes
 
On the Construction of Cantor like Sets
	On the Construction of Cantor like Sets	On the Construction of Cantor like Sets
On the Construction of Cantor like Sets
theijes
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
ijceronline
 

Similar to Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2 (20)

Patterns in Series
Patterns in SeriesPatterns in Series
Patterns in Series
 
Numeros reales, inecuaciones y desigualdades
Numeros reales, inecuaciones y desigualdadesNumeros reales, inecuaciones y desigualdades
Numeros reales, inecuaciones y desigualdades
 
1Q LP 3.4 Venn Diagram & Set Operation (Complement and DIfference).ppsx
1Q LP 3.4  Venn Diagram & Set Operation (Complement and DIfference).ppsx1Q LP 3.4  Venn Diagram & Set Operation (Complement and DIfference).ppsx
1Q LP 3.4 Venn Diagram & Set Operation (Complement and DIfference).ppsx
 
PO_groupVI_Term assignment.pptx
PO_groupVI_Term assignment.pptxPO_groupVI_Term assignment.pptx
PO_groupVI_Term assignment.pptx
 
Arithmetic sequence
Arithmetic sequenceArithmetic sequence
Arithmetic sequence
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 
math 10 aug. 6, 2023.pptx
math 10 aug. 6, 2023.pptxmath 10 aug. 6, 2023.pptx
math 10 aug. 6, 2023.pptx
 
Correct sorting with Frama-C
Correct sorting with Frama-CCorrect sorting with Frama-C
Correct sorting with Frama-C
 
Estructura Discreta I
Estructura Discreta IEstructura Discreta I
Estructura Discreta I
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
Unit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptxUnit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptx
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
 
Diploma_Semester-II_Advanced Mathematics_Definite Integration
Diploma_Semester-II_Advanced Mathematics_Definite IntegrationDiploma_Semester-II_Advanced Mathematics_Definite Integration
Diploma_Semester-II_Advanced Mathematics_Definite Integration
 
Integers
IntegersIntegers
Integers
 
Integers
IntegersIntegers
Integers
 
Part 1 sequence and arithmetic progression
Part 1 sequence and arithmetic progressionPart 1 sequence and arithmetic progression
Part 1 sequence and arithmetic progression
 
Lec38
Lec38Lec38
Lec38
 
On the Construction of Cantor like Sets
On the Construction of Cantor like SetsOn the Construction of Cantor like Sets
On the Construction of Cantor like Sets
 
On the Construction of Cantor like Sets
	On the Construction of Cantor like Sets	On the Construction of Cantor like Sets
On the Construction of Cantor like Sets
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
 

Recently uploaded

NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 

Recently uploaded (20)

NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 

Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2

  • 1.
  • 2. Prefix sum It is a simple yet powerful technique that allows to perform fast calculation on the sum of elements in a given range (called contiguous segments of array).
  • 3. Prefix sum It is a simple yet powerful technique that allows to perform fast calculation on the sum of elements in a given range (called contiguous segments of array). 6 3 -2 4 -1 0 -5 Example - A[ ] =
  • 4. Prefix sum It is a simple yet powerful technique that allows to perform fast calculation on the sum of elements in a given range (called contiguous segments of array). 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 A[ ] =
  • 5. Prefix sum It is a simple yet powerful technique that allows to perform fast calculation on the sum of elements in a given range (called contiguous segments of array). 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5Prefix Sum Array - A[ ] = A[ ] =
  • 6. Prefix sum 6 3 -2 4 -1 0 -5 0 1 2 3 4 5 6 6 9 -2 4 -1 0 -5 6 9 7 4 -1 0 -5 6 9 7 11 -1 0 -5 6 9 7 11 10 0 -5 6 9 7 11 10 10 5 6 9 7 11 10 10 -5 Example - i = A[ ] =
  • 7. Prefix sum 6 3 -2 4 -1 0 -5 0 1 2 3 4 5 6 6 9 -2 4 -1 0 -5 6 9 7 4 -1 0 -5 6 9 7 11 -1 0 -5 6 9 7 11 10 0 -5 6 9 7 11 10 10 5 6 9 7 11 10 10 -5 for( int i=1 ; i<7 ; i++ ){ A[i] = A[i]+A[i-1]; } n Example - i = A[ ] =
  • 8. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6
  • 9. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = i = 0 1 2 3 4 5 6
  • 10. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = i = 0 1 2 3 4 5 6
  • 11. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = Ans : 10 (=A[4]) O(1) i = 0 1 2 3 4 5 6
  • 12. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = Ans : 10 (=A[4]) O(1) Calculate the sum between range [0, 6] ? i = 0 1 2 3 4 5 6
  • 13. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = Ans : 10 (=A[4]) O(1) Calculate the sum between range [0, 6] ? i = 0 1 2 3 4 5 6
  • 14. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = Ans : 10 (=A[4]) O(1) Calculate the sum between range [0, 6] ? Ans : 5 (=A[6]) O(1) i = 0 1 2 3 4 5 6
  • 15. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = Ans : 10 (=A[4]) O(1) Calculate the sum between range [0, 6] ? Ans : 5 (=A[6]) O(1) Calculate the sum between range [2, 6] ? i = 0 1 2 3 4 5 6
  • 16. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = Ans : 10 (=A[4]) O(1) Calculate the sum between range [0, 6] ? Ans : 5 (=A[6]) O(1) Calculate the sum between range [2, 6] ? sum between range [0, 6] i = 0 1 2 3 4 5 6
  • 17. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = Ans : 10 (=A[4]) O(1) Calculate the sum between range [0, 6] ? Ans : 5 (=A[6]) O(1) Calculate the sum between range [2, 6] ? sum between range [0, 6] sum between range [0, 6] = sum between range [2, 6] sum between range [0, 1] + i = 0 1 2 3 4 5 6
  • 18. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = Ans : 10 (=A[4]) O(1) Calculate the sum between range [0, 6] ? Ans : 5 (=A[6]) O(1) Calculate the sum between range [2, 6] ? sum between range [0, 6] sum between range [0, 6] = sum between range [2, 6] sum between range [0, 1] + A[6] = A[1] + sum between range [2, 6] A[6] - A[1] = sum between range [2, 6] i = 0 1 2 3 4 5 6
  • 19. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = Ans : 10 (=A[4]) O(1) Calculate the sum between range [0, 6] ? Ans : 5 (=A[6]) O(1) Calculate the sum between range [2, 6] ? sum between range [0, 6] sum between range [0, 6] = sum between range [2, 6] sum between range [0, 1] + A[6] = A[1] + sum between range [2, 6] A[6] - A[1] = sum between range [2, 6] sum between range [2, 6] = A[6] - A[1] i = 0 1 2 3 4 5 6
  • 20. Prefix sum 6 3 -2 4 -1 0 -5 Example - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - Calculate the sum between range [0, 4] ? A[ ] = A[ ] = Ans : 10 (=A[4]) O(1) Calculate the sum between range [0, 6] ? Ans : 5 (=A[6]) O(1) Calculate the sum between range [2, 6] ? sum between range [0, 6] sum between range [0, 6] = sum between range [2, 6] sum between range [0, 1] + A[6] = A[1] + sum between range [2, 6] A[6] - A[1] = sum between range [2, 6] sum between range [2, 6] = A[6] - A[1] sum between range [2, 6] = 5 - 9 sum between range [2, 6] = - 4 i = 0 1 2 3 4 5 6
  • 21. Prefix sum 6 3 -2 4 -1 0 -5 Generalization - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6
  • 22. Prefix sum 6 3 -2 4 -1 0 -5 Generalization - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6 A[2, 6]
  • 23. Prefix sum 6 3 -2 4 -1 0 -5 Generalization - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6 A[6] A[2, 6]
  • 24. Prefix sum 6 3 -2 4 -1 0 -5 Generalization - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6 A[1] A[6] A[1] A[2, 6]
  • 25. Prefix sum 6 3 -2 4 -1 0 -5 Generalization - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6 A[2, 6] = A[6] - A[1] A[6] A[1] A[2, 6]
  • 26. Prefix sum 6 3 -2 4 -1 0 -5 Generalization - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6 To calculate the sum between range [I, j] A[2, 6] = A[6] - A[1] A[6] A[1] A[2, 6]
  • 27. Prefix sum 6 3 -2 4 -1 0 -5 Generalization - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6 Formula - To calculate the sum between range [I, j] A[2, 6] = A[6] - A[1] A[i, j] = A[j] - A[i - 1] A[6] A[1] A[2, 6]
  • 28. Prefix sum 6 3 -2 4 -1 0 -5 Generalization - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6 Formula - To calculate the sum between range [I, j] A[2, 6] = A[6] - A[1] Calculate the sum between range [3, 5] ? A[i, j] = A[j] - A[i - 1] A[6] A[1] A[2, 6]
  • 29. Prefix sum 6 3 -2 4 -1 0 -5 Generalization - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6 Formula - To calculate the sum between range [I, j] A[2, 6] = A[6] - A[1] Calculate the sum between range [3, 5] ? A[3, 5] = A[5] - A[3 - 1] A[3, 5] = A[5] - A[2] A[3, 5] = 10 - 7 A[3, 5] = 3 A[i, j] = A[j] - A[i - 1] A[6] A[1] A[2, 6]
  • 30. Prefix sum 6 3 -2 4 -1 0 -5 Generalization - i = 0 1 2 3 4 5 6 6 9 7 11 10 10 5 Prefix Sum Array - A[ ] = A[ ] = i = 0 1 2 3 4 5 6 A[6] A[1] A[2, 6] Formula - To calculate the sum between range [I, j] A[2, 6] = A[6] - A[1] Calculate the sum between range [3, 5] ? A[3, 5] = A[5] - A[3 - 1] A[3, 5] = A[5] - A[2] A[3, 5] = 10 - 7 A[3, 5] = 3 O(1) A[i, j] = A[j] - A[i - 1]
  • 31. • To calculate prefix sum array of n size array Time complexity - O(n) Analysis of Algorithm - • Time taken to perform range sum query is - Time complexity - O(1)
  • 32. Analysis of Algorithm - • Total time taken to pre process the n size array and to perform range query is - Time complexity - + O(1)O(n) ~ O(n) • To calculate prefix sum array of n size array Time complexity - O(n) • Time taken to perform range sum query is - Time complexity - O(1)
  • 33. • It takes O(n) time to calculate prefix sum array of n size array. Key takeaway from this lesson - • It takes O(1) time to perform range sum query on n size array.
  • 34. Here is the link to video tutorial :- https://youtu.be/pVS3yhlzrlQ