Skip to content

Commit 6641791

Browse files
authored
Merge pull request #539 from sidparashar2001/issue#439
RunningSumOf1dArray
2 parents d6eddaf + 7d9bcc5 commit 6641791

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Arrays/RunningSumOf1dArray.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*Question:-
2+
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
3+
4+
Return the running sum of nums.
5+
6+
7+
8+
Example 1:
9+
10+
Input: nums = [1,2,3,4]
11+
Output: [1,3,6,10]
12+
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
13+
Example 2:
14+
15+
Input: nums = [1,1,1,1,1]
16+
Output: [1,2,3,4,5]
17+
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
18+
Constraints:
19+
20+
1 <= nums.length <= 1000
21+
-10^6 <= nums[i] <= 10^6
22+
*/
23+
#include<bits/stdc++.h>
24+
using namespace std;
25+
vector<int> runningSum(vector<int>& nums) {
26+
for(int i=1;i<nums.size();i++){
27+
nums[i]+=nums[i-1];
28+
}
29+
return nums;//returning the array
30+
}
31+
int main(){
32+
vector<int> nums;//to store elements
33+
int n,element;
34+
cin>>n;
35+
for (int i = 0; i < n; i++)
36+
{
37+
cin>>element;
38+
nums.push_back(element);//storeing the elements
39+
}
40+
vector<int> result=runningSum(nums);//calling the fuction to return the running sum of array
41+
for (int i = 0; i < result.size(); i++)
42+
{
43+
cout<<result[i]<<",";//print the elements of running sum array
44+
}
45+
46+
return 0;
47+
}

0 commit comments

Comments
 (0)