A freight yard numbers the boxcars of a train from 1 to n, front to back. Each boxcar i carries nums[i] tonnes of cargo. For any contiguous run of boxcars [l, r] (1 <= l <= r <= n), define its total weight as the sum of nums[l] through nums[r]. A yard inspector calls the run [l, r] "digit-balanced" if the sum of the decimal digits of the run's total weight is exactly equal to the sum of the decimal digits of l plus the sum of the decimal digits of r (for example, the digit sum of 108 is 1+0+8=9, and the digit sum of 0 is 0). Count how many digit-balanced runs exist in the train.
Print a single integer: the total number of digit-balanced runs [l, r] (over all 1 <= l <= r <= n).
Example 1
Input
4 2 3 100 5
Expected
1
Explanation
Checking run [1,1]: total weight is 2, whose digit sum is 2, and digitSum(1)+digitSum(1)=1+1=2 — a match. Every other run (checked exhaustively: [1,2],[1,3],[1,4],[2,2],[2,3],[2,4],[3,3],[3,4],[4,4]) fails to match. So exactly 1 run is digit-balanced.
Example 2
Input
2 1 1
Expected
0
Explanation
Run [1,1]: total=1, digitSum=1, target=digitSum(1)+digitSum(1)=2 — no match. Run [1,2]: total=2, digitSum=2, target=digitSum(1)+digitSum(2)=3 — no match. Run [2,2]: total=1, digitSum=1, target=digitSum(2)+digitSum(2)=4 — no match. No run is digit-balanced, so the answer is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →