-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81e87fa
commit 1fdd0c0
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
|
||
/* | ||
return a vector of prefix sum of the inputed vector | ||
Parameter: | ||
- vector long long | ||
Return: | ||
- vector long long of size inputed vector + 1(cause first element will be 0) | ||
*/ | ||
vector<long long> prefix_sum(vector<long long> v) | ||
{ | ||
// initializing prefix sum vector | ||
long long n = v.size(); | ||
vector<long long> psum(n + 1, 0); | ||
for (long long i = 1; i <= n; i++) | ||
{ | ||
psum[i] = psum[i - 1] + v[i - 1]; | ||
} | ||
return psum; | ||
} | ||
|
||
int main() | ||
{ | ||
vector<long long> v = {4, 3, 2, 5}; | ||
for (long long x : prefix_sum(v)) | ||
cout << x << ' '; | ||
return 0; | ||
} |