Suppose we have an array of numbers {x1, x2, x3 ...... xn}. If we select an element Xi from this array, how many sub-arrays exist with this element included?
Asked
Active
Viewed 26 times
0
Omar S
- 105
-
Contiguous. I think sub-array implies that. Non-contiguous would probably be a subsequence – Omar S Sep 01 '19 at 06:16
-
Don't use ${ \ldots }$ for (finite or infinite) sequences. Use $( \ldots )$ instead. Also, use MathJax to format your posts. – parsiad Sep 01 '19 at 06:26
1 Answers
1
Any contiguous subarray looks like $(x_a,x_{a+1},\ldots,x_{i-1},x_i,x_{i+1}\ldots,x_{b-1},x_b)$ where $a\leq i\leq b$. That gives you $i$ ways to choose $a$ and $n-i+1$ ways to choose $b$. Take the product to get $i(n-i+1)$ subarrays.
parsiad
- 25,154