As a data analyst at Zoblik International, you're tasked with monitoring the performance of various financial instruments. For a particular stock, you've collected its net daily change (gain or loss) over several trading days. To get a better understanding of the stock's overall trajectory and total return up to any given day, you need to calculate the cumulative daily returns.
The cumulative daily return at any specific day `i` is defined as the sum of all net daily changes from the first day (index 0) up to and including day `i`.
For example, if the daily changes are [+1, +2, +3, +4]:
+1.+1 + +2 = +3.+1 + +2 + +3 = +6.+1 + +2 + +3 + +4 = +10.Your task is to implement a function that takes an array representing the net daily changes of a stock and returns a new array where each element is the cumulative daily return up to that point.
The first line of input contains an integer, `T`, representing the number of test cases.
For each test case:
For each test case, print a single line containing `N` space-separated integers, which are the cumulative daily returns for the given stock.
0 <= T <= 1000 (Number of test cases)1 <= N <= 10000 (Number of days/elements in the array)-10^4 <= array element <= 10^4 (Value of each daily change)0 <= T <= 1000; 1 <= N <= 10000; -10^4<= array element <= 10^4
Sample Input:
3 4 1 2 3 4 5 1 1 1 1 1 5 1 3 5 7 9Sample Output:
1 3 6 10 1 2 3 4 5 1 4 9 16 25Explanation for Sample 1:
Input Array:
[1, 2, 3, 4]
- First element:
1(Cumulative sum up to index 0 is 1)- Second element:
1 + 2 = 3(Cumulative sum up to index 1 is 3)- Third element:
1 + 2 + 3 = 6(Cumulative sum up to index 2 is 6)- Fourth element:
1 + 2 + 3 + 4 = 10(Cumulative sum up to index 3 is 10)Result:
[1, 3, 6, 10]Explanation for Sample 2:
Input Array:
[1, 1, 1, 1, 1]
- Cumulative sums:
1,1+1=2,2+1=3,3+1=4,4+1=5Result:
[1, 2, 3, 4, 5]
Arrays
Start coding and your submissions will appear here.