Find sum of lengths of all subarrays of an array consisting particular index of the given array?
index may be any
Any formula for this?
Bro i asked sum of lengths and not count of subarrays there is difference in both
Sorry, I misread it.
If you want to find the sum of lengths of all subarrays which contain index k
: Remove the sum of lengths of all subarrays which don’t contain the index k
from the sum of lengths of all possible subarrays.
Sum of length of all subarrays for an array of size n = cnt(n) = n * (n + 1) * (n + 2) / 6
finalAns = cnt(n) - (cnt(k - 1) + cnt(n - k))
I have assumed 1-based indexing.