You are given a 0-indexed array of n integers differences, which describes the differences between each pair of consecutive integers of a hidden sequence of length (n + 1). More formally, call the hidden sequence hidden, then we have that differences[i] = hidden[i + 1] - hidden[i].

You are further given two integers lower and upper that describe the inclusive range of values [lower, upper] that the hidden sequence can contain.


	For example, given differences = [1, -3, 4], lower = 1, upper = 6, the hidden sequence is a sequence of length 4 whose elements are in between 1 and 6 (inclusive).

	
		[3, 4, 1, 5] and [4, 5, 2, 6] are possible hidden sequences.
		[5, 6, 3, 7] is not possible since it contains an element greater than 6.
		[1, 2, 3, 4] is not possible since the differences are not correct.
	
	


Return the number of possible hidden sequences there are. If there are no possible sequences, return 0.

&nbsp;
Example 1:

Input: differences = [1,-3,4], lower = 1, upper = 6
Output: 2
Explanation: The possible hidden sequences are:
- [3, 4, 1, 5]
- [4, 5, 2, 6]
Thus, we return 2.


Example 2:

Input: differences = [3,-4,5,1,-2], lower = -4, upper = 5
Output: 4
Explanation: The possible hidden sequences are:
- [-3, 0, -4, 1, 2, 0]
- [-2, 1, -3, 2, 3, 1]
- [-1, 2, -2, 3, 4, 2]
- [0, 3, -1, 4, 5, 3]
Thus, we return 4.


Example 3:

Input: differences = [4,-7,2], lower = 3, upper = 6
Output: 0
Explanation: There are no possible hidden sequences. Thus, we return 0.


&nbsp;
Constraints:


	n == differences.length
	1 &lt;= n &lt;= 105
	-105 &lt;= differences[i] &lt;= 105
	-105 &lt;= lower &lt;= upper &lt;= 105


class Solution:
    def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
        l = [0]
        for i in differences:
            l.append(l[-1]+i)
        return max(0,(upper-lower+1)-(max(l)-min(l)))
class Solution {
    public int numberOfArrays(int[] differences, int lower, int upper) {
        ArrayList<Integer> ans = new ArrayList<>();
        ans.add(lower); 
        int mn = lower;
        int mx = lower;
        
        for (int i = 0; i < differences.length; i++) {
            int d = differences[i];
            ans.add(d + ans.get(ans.size() - 1));
            mn = Math.min(mn, ans.get(ans.size() - 1));
            mx = Math.max(mx, ans.get(ans.size() - 1));
        }

        int add = lower - mn;
        
        for (int i = 0; i < ans.size(); i++) {
            ans.set(i, ans.get(i) + add);
        }
        
        for (int i = 0; i < ans.size(); i++) {
            if (ans.get(i) < lower ||  upper < ans.get(i)) {
                return 0;
            }
        }
        
        int add2 = upper - mx;
        
        return add2 - add + 1;
    }
}
using ll = long long int;
class Solution {
    public:
    int numberOfArrays(vector<int>& differences, int lower, int upper) {
        vector<ll> ans; 
        ans.push_back(lower); 
        ll mn = lower;
        ll mx = lower;
        for (const auto& d: differences) {
            ans.push_back(d + ans.back());
            mn = min(mn, ans.back());
            mx = max(mx, ans.back());
        }

        ll add = lower - mn;
        
        for (auto& i: ans) i += add;
        for (auto& i: ans) if (i < lower or upper < i) return 0;
        
        ll add2 = upper - mx;
        
        return add2 - add + 1;
    }
}; 
var numberOfArrays = function(differences, lower, upper) {
    let temp = 0;
    let res = 0;
    let n = lower;

    for (let i = 0; i < differences.length; i++) {
        temp += differences[i];
        differences[i] = temp;
    }

    const min = Math.min(...differences);
    const max = Math.max(...differences);

    while (n <= upper) {
        if (n + min >= lower && n + max <= upper) res++;
        n++;
    }
    return res;
};
Count the Hidden Sequences