0

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?

Omar S
  • 105

1 Answers1

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