 Open in App
Not now

# accumulate() and partial_sum() in C++ STL : Numeric header

• Difficulty Level : Easy
• Last Updated : 18 Mar, 2023

The numeric header is part of the numeric library in C++ STL. This library consists of basic mathematical functions and types, as well as optimized numeric arrays and support for random number generation. Some of the functions in the numeric header:

• iota
• accumulate
• reduce
• inner_product
• partial_sum etc.

This article explains accumulate() and partial_sum() in the numeric header which can be used during competitive programming to save time and effort.
1) accumulate(): This function returns the sum of all the values lying in a range between [first, last) with the variable sum. We usually find out the sum of elements in a particular range or a complete array using a linear operation which requires adding all the elements in the range one by one and storing it into some variable after each iteration.

Syntax:

`accumulate(first, last, sum);`

or

`accumulate(first, last, sum, myfun); `

Parameters:

• first, last: first and last elements of range whose elements are to be added
• sum:  initial value of the sum
• myfun: a function for performing any specific task.

For example, we can find the product of elements between the first and last.

## CPP

 `// C++ program to demonstrate working of accumulate()``#include ``#include ``using` `namespace` `std;` `// User defined function``int` `myfun(``int` `x, ``int` `y)``{``    ``// for this example we have taken product``    ``// of adjacent numbers``    ``return` `x * y;``}` `int` `main()``{``    ``// Initialize sum = 1``    ``int` `sum = 1;``    ``int` `a[] = { 5, 10, 15 };` `    ``// Simple default accumulate function``    ``cout << ``"\nResult using accumulate: "``;``    ``cout << accumulate(a, a + 3, sum);` `    ``// Using accumulate function with``    ``// defined function``    ``cout << ``"\nResult using accumulate with"``            ``"user-defined function: "``;``    ``cout << accumulate(a, a + 3, sum, myfun);` `    ``// Using accumulate function with``    ``// pre-defined function``    ``cout << ``"\nResult using accumulate with "``            ``"pre-defined function: "``;``    ``cout << accumulate(a, a + 3, sum, std::minus<``int``>());` `    ``return` `0;``}`

Output

```Result using accumulate: 31
Result using accumulate withuser-defined function: 750
Result using accumulate with pre-defined function: -29```

Note: For adding larger values beyond the int range the sum should be initialized with 0ll or a user-defined sum with the suffix ll,  else the sum will be overflown. (here ll refers to long long int)
Example: accumulate(a,a+n,0ll)

See this Example Problem for more reference: Sum of all elements between k1’th and k2’th smallest elements

2) partial_sum( ): This function assigns a partial sum of the corresponding elements of an array to every position of the second array. It returns the partial sum of all the sets of values lying between [first, last) and stores it in another array b.
For example, if x represents an element in [first, last) and y represents an element in the result, the ys can be calculated as:

```y0 = x0
y1 = x0 + x1
y2 = x0 + x1 + x2
y3 = x0 + x1 + x2 + x3
y4 = x0 + x1 + x2 + x3 + x4```

Syntax:

`partial_sum(first, last, b);`

or

`partial_sum(first, last, b, myfun);`

Parameters:

• first, last: first and last element of the range whose elements are to be added
• b: index of array where  corresponding partial sum will be stored
• myfun: a user-defined function for performing any specific task

## CPP

 `// C++ program to demonstrate working of partial_sum()``#include ``#include ``using` `namespace` `std;` `// user defined function``int` `myfun(``int` `x, ``int` `y)``{``    ``// the sum of element is twice of its``    ``// adjacent element``    ``return` `x + 2 * y;``}` `int` `main()``{``    ``int` `a[] = { 1, 2, 3, 4, 5 };``    ``int` `b;` `    ``// Default function``    ``partial_sum(a, a + 5, b);` `    ``cout << ``"Partial Sum - Using Default function: "``;``    ``for` `(``int` `i = 0; i < 5; i++)``        ``cout << b[i] << ``' '``;``    ``cout << ``'\n'``;` `    ``// Using user defined function``    ``partial_sum(a, a + 5, b, myfun);` `    ``cout << ``"Partial sum - Using user defined function: "``;``    ``for` `(``int` `i = 0; i < 5; i++)``        ``cout << b[i] << ``' '``;``    ``cout << ``'\n'``;` `    ``return` `0;``}`

Output

```Partial Sum - Using Default function: 1 3 6 10 15
Partial sum - Using user defined function: 1 5 11 19 29 ```

Explanation of code :

without myfun:

simply, ith  element of a array + i-1th element of array b makes equal to ith element of b

i.e. b[i]=a[i]+b[i-1]

b =1+no ele=> 1

b =2+1=> 3

b= 3+3=> 6

b=6+4=10 and   so on

with myfun:

in the same way :

b[i]=a[i]+2*b[i-1]

means b= 1+2*0 => 1

b = 1+2*2=> 5

b= 5+3*2=> 11

b=11+4*2=> 19 and so on upto last.