Market Trend Analyzer: Cumulative Daily Returns

🎯 Level: Easy Score: 30 ⏱️ Estimated Time: 30 mins
📜 Problem Description

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]:

  • On day 0, the cumulative return is +1.
  • On day 1, the cumulative return is +1 + +2 = +3.
  • On day 2, the cumulative return is +1 + +2 + +3 = +6.
  • On day 3, the cumulative return is +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.

Input Format

The first line of input contains an integer, `T`, representing the number of test cases.

For each test case:

  • The first value is an integer `N`, indicating the number of days (elements in the array).
  • The following `N` integers represent the net daily changes for the stock, separated by spaces.

Output Format

For each test case, print a single line containing `N` space-separated integers, which are the cumulative daily returns for the given stock.

Constraints

  • 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)
📌 Constraints
0 <= T <= 1000; 1 <= N <= 10000; -10^4<= array element <= 10^4
📝 Sample Input/Output
Sample Input:
3
4 1 2 3 4
5 1 1 1 1 1
5 1 3 5 7 9
Sample Output:
1 3 6 10
1 2 3 4 5
1 4 9 16 25
Explanation 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=5

Result: [1, 2, 3, 4, 5]

💡 Hints
  • 💡 Think about how each element in the cumulative sum array relates to the *previous* cumulative sum and the *current* original array element.
  • 💡 An iterative approach, maintaining a running total, is very efficient for this problem.
  • 💡 Consider initializing your running total with 0 and adding elements one by one, or directly using the first element of the input array as the start of your cumulative sum.
📚 Topics

Arrays

😕 No submissions found

Start coding and your submissions will appear here.