910. Sum of Consecutive Subarrays

An array arr of length n is consecutive if one of the following holds:

  • arr[i] - arr[i - 1] == 1 for all 1 <= i < n.
  • arr[i] - arr[i - 1] == -1 for all 1 <= i < n.

For example, [4, 5, 6] is a consecutive array with a value of 4 + 5 + 6 = 15, and [10, 9] is another with a value of 10 + 9 = 19. However, [4, 6, 5] and [8, 6] are not consecutive.

Given an integer array nums, return the sum of the values of all consecutive subarrays. The value of an array is the sum of its elements.

Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: nums = [2, 3, 4]

Output: 30

Explanation: The consecutive subarrays are: [2], [3], [4], [2, 3], [3, 4], [2, 3, 4].

Their sum is 2 + 3 + 4 + 5 + 7 + 9 = 30.

Example 2:

Input: nums = [2, 4, 6, 8]

Output: 20

Explanation: The consecutive subarrays are: [2], [4], [6], [8].

Their sum is 2 + 4 + 6 + 8 = 20.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [9, 8, 2, 3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int getSum(vector<int>& nums) {
}
};
 
Test Case

Input:

Nums