(10) I have an array of n integers. How many subarrays are contained in my array? - Quora
For an array with distinct elements, it would be same as the number of ways of choosing two indices i,j such that the following conditions are met1. i <= j,
2. i,j >= 0 (assumption indices run from 0 to n-1)
3. i,j < n
When i = 0, j can run from 0 to n-1 i.e. n choices
When i =1, j can run from 1 to n-1
.
.
When i =n-1 j can be only n-1
Summing the terms up gives us n + (n-1) + (n-2) + ... + 1
=n(n+1)/2
However for repeated elements the answer will vary with the contents of the array.
I will try to find some generalized equation for the same, but I doubt there is one.
Read full article from (10) I have an array of n integers. How many subarrays are contained in my array? - Quora
No comments:
Post a Comment