Related Articles
Prefix Sum Array – Implementation and Applications in Competitive Programming
• Difficulty Level : Medium
• Last Updated : 03 Feb, 2021

Given an array arr[] of size n, its prefix sum array is another array prefixSum[] of same size such that the value of prefixSum[i] is arr + arr + arr … arr[i].

Examples :

```Input  : arr[] = {10, 20, 10, 5, 15}
Output : prefixSum[] = {10, 30, 40, 45, 60}

Explanation : While traversing the array, update
the element by adding it with its previous element.
prefixSum = 10,
prefixSum = prefixSum + arr = 30,
prefixSum = prefixSum + arr = 40 and so on.```

To fill prefix sum array, we run through index 1 to last and keep on adding present element with previous value in prefix sum array.
Below is the implementation :

## C++

 `// C++ program for Implementing``// prefix sum array``#include ``using` `namespace` `std;` `// Fills prefix sum array``void` `fillPrefixSum(``int` `arr[], ``int` `n, ``int` `prefixSum[])``{``    ``prefixSum = arr;` `    ``// Adding present element``    ``// with previous element``    ``for` `(``int` `i = 1; i < n; i++)``        ``prefixSum[i] = prefixSum[i - 1] + arr[i];``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 10, 4, 16, 20 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``int` `prefixSum[n];` `    ``fillPrefixSum(arr, n, prefixSum);``    ``for` `(``int` `i = 0; i < n; i++)``        ``cout << prefixSum[i] << ``" "``;``}`

## Java

 `// Java Program for Implementing``// prefix sum arrayclass``class` `Prefix {``    ``// Fills prefix sum array``    ``static` `void` `fillPrefixSum(``int` `arr[], ``int` `n,``                              ``int` `prefixSum[])``    ``{``        ``prefixSum[``0``] = arr[``0``];` `        ``// Adding present element``        ``// with previous element``        ``for` `(``int` `i = ``1``; i < n; ++i)``            ``prefixSum[i] = prefixSum[i - ``1``] + arr[i];``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = { ``10``, ``4``, ``16``, ``20` `};``        ``int` `n = arr.length;``        ``int` `prefixSum[] = ``new` `int``[n];` `        ``fillPrefixSum(arr, n, prefixSum);` `        ``for` `(``int` `i = ``0``; i < n; i++)``            ``System.out.print(prefixSum[i] + ``" "``);``        ``System.out.println(``""``);``    ``}``}` `// This Code is Contributed by Saket Kumar`

## Python3

 `# Python Program for Implementing``# prefix sum array` `# Fills prefix sum array``def` `fillPrefixSum(arr, n, prefixSum):` `    ``prefixSum[``0``] ``=` `arr[``0``]` `    ``# Adding present element``    ``# with previous element``    ``for` `i ``in` `range``(``1``, n):``        ``prefixSum[i] ``=` `prefixSum[i ``-` `1``] ``+` `arr[i]` `# Driver code``arr ``=``[``10``, ``4``, ``16``, ``20` `]``n ``=` `len``(arr)` `prefixSum ``=` `[``0` `for` `i ``in` `range``(n ``+` `1``)]` `fillPrefixSum(arr, n, prefixSum)` `for` `i ``in` `range``(n):``    ``print``(prefixSum[i], ``" "``, end ``=``"")` `# This code is contributed``# by Anant Agarwal.`

## C#

 `// C# Program for Implementing``// prefix sum arrayclass``using` `System;` `class` `GFG {``    ``// Fills prefix sum array``    ``static` `void` `fillPrefixSum(``int``[] arr, ``int` `n,``                              ``int``[] prefixSum)``    ``{``        ``prefixSum = arr;` `        ``// Adding present element``        ``// with previous element``        ``for` `(``int` `i = 1; i < n; ++i)``            ``prefixSum[i] = prefixSum[i - 1] + arr[i];``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``int``[] arr = { 10, 4, 16, 20 };``        ``int` `n = arr.Length;``        ``int``[] prefixSum = ``new` `int``[n];` `        ``fillPrefixSum(arr, n, prefixSum);` `        ``for` `(``int` `i = 0; i < n; i++)``            ``Console.Write(prefixSum[i] + ``" "``);``        ``Console.Write(``""``);``    ``}``}` `// This Code is Contributed by nitin mittal`

## PHP

 ``

Output:

`10 14 30 50`

Applications :

An Example Problem :
Consider an array of size n with all initial values as 0. Perform given ‘m’ add operations from index ‘a’ to ‘b’ and evaluate highest element in array. An add operation adds 100 to all elements from a to b (both inclusive).
Example :

```Input : n = 5 // We consider array {0, 0, 0, 0, 0}
m = 3.
a = 2, b = 4.
a = 1, b = 3.
a = 1, b = 2.
Output : 300

Explanation :

After I operation -
A : 0 100 100 100 0

After II operation -
A : 100 200 200 100 0

After III operation -
A : 200 300 200 100 0

Highest element : 300```

A simple approach is running a loop ‘m’ times. Inputting a and b and running a loop from a to b, adding all elements by 100.
Efficient approach using Prefix Sum Array

```1 : Run a loop for 'm' times, inputting 'a' and 'b'.
2 : Add 100 at index 'a-1' and subtract 100 from index 'b'.
3 : After completion of 'm' operations, compute the prefix sum array.
4 : Scan the largest element and we're done.```

What we did was adding 100 at ‘a’ because this will add 100 to all elements while taking prefix sum array. Subtracting 100 from ‘b+1’ will reverse the changes made by adding 100 to elements from ‘b’ onward.
For better understanding :

```After I operation -
A : 0 100 0 0 -100
Prefix Sum Array : 0 100 100 100 0

After II operation -
A : 100 100 0 -100 -100
Prefix Sum Array : 100 200 200 100 0

After III operation -
A : 200 100 -100 -100 -100
Prefix Sum Array : 200 300 200 100 0

Final Prefix Sum Array : 200 300 200 100 0

The required highest element : 300```

## C++14

 `#include ``using` `namespace` `std;` `int` `find(``int` `m, vector> q)``{``    ``int` `mx = 0;``    ``vector<``int``> pre(5,0);``    ` `    ``for` `(``int` `i = 0; i < m; i++)``    ``{   ``        ``// take input a and b``        ``int` `a = q[i].first, b = q[i].second;``      ` `        ``// add 100 at first index and``        ``// subtract 100 from last index``      ` `        ``// pre becomes 100``        ``pre[a-1] += 100;``         ` `        ``// pre becomes -100 and this``        ``pre[b] -=100;   ``        ``// continues m times as we input diff. values of a and b``    ``}``    ``for` `(``int` `i = 1; i < 5; i++)``    ``{``        ``// add all values in a cumulative way``        ``pre[i] += pre[i - 1];``        ` `        ``// keep track of max value``        ``mx = max(mx, pre[i]);``    ``}``     ` `                               ` `    ``return` `mx;``}` `// Driver Code``int` `main()``{``    ` `    ``int` `m = 3;``    ``vector> q = {{2,4},{1,3},{1,2}};``  ` `  ` `    ``// Function call``    ``cout<< find(m,q);``    ``return` `0;``}`

## Python3

 `# Python implementation of the approach``def` `find( m, q):``    ``mx ``=` `0``    ``pre ``=` `[``0` `for` `i ``in` `range``(``5``) ]``    ` `    ``for` `i ``in` `range``(m):``        ``# take input a and b``        ``a,b ``=` `q[i][``0``], q[i][``1``]``      ` `        ``# add 100 at first index and``        ``# subtract 100 from last index``      ` `        ``# pre becomes 100``        ``pre[a``-``1``] ``+``=` `100``         ` `        ``# pre becomes -100 and this``        ``pre[b] ``-``=``100``; ``        ` `        ``# continues m times as we input diff. values of a and b``    ``for` `i ``in` `range``(``1``,``5``):``      ` `        ``# add all values in a cumulative way``        ``pre[i] ``+``=` `pre[i ``-` `1``]``        ` `        ``# keep track of max value``        ``mx ``=` `max``(mx, pre[i])                            ``    ``return` `mx``  ` `# Driver Code``m ``=` `3``q ``=` `[[``2``,``4``],[``1``,``3``],[``1``,``2``]]` `# Function call``print``(find(m,q))` `# This code is contributed by rohitsingh07052`
Output
`300`

Recent Articles on Prefix Sum Technique
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.