doubt in calculating expectations

i had recently learned expectation in math and trying to solve :

i read the editorial and able to understand it but i want to know where my approach is lacking?
my approach :

E(x) = ((1 + E(x-1))*(1/n) + (2 + E(x-1))*(1/n^2) + ...........)*n

i thought that if we can take any song from list in one attempt(prob : 1/n) and then have to choose (n-1) different songs + (if i choose that song in two attempt (1/n^2) and then choose n-1 left songs E(x-1)) + ........
and finally i can choose any of the songs from the list as my current song(song that i have choosing 1 time+ 2 times+…) so, multiply my answer by n!

can anyone please explain where i am going wrong!

if possible please give some questions to practice :slight_smile:

This question is coupon collector problem.
You can refer this video for getting ur doubts cleared:

You could find more sums on spoj.Sorry i dont have its link .

E(x) = ((1 + E(x-1))(1/n) + (2 +
(1/n^2) + …)*n

What is x here? I am assuming that E(x) = expected number of songs guys have to listen to until x different songs have being played.

if i choose that song in two attempt

This is wrong. it would be \frac{n-1}{n} \times n

finally i can choose any of the songs from the list as my current song(song that i have choosing 1 time+ 2 times+…) so, multiply my answer by n!

First you can’t choose any song; you can only choose among n - (x-1) songs, the rest are already there. Second you cant just multiply like that.

Correcting your approach:

E(x) = \sum_{i=1}^{\infty}(E(x-1) + i) * \bigg( \frac{x-1}{n} \bigg)^{i-1} * \frac{n-(x-1)}{n}
E(x) = \frac{n-(x-1)}{n}\sum_{i=1}^{\infty}(E(x-1) + i) * \bigg( \frac{x-1}{n} \bigg)^{i-1}
E(x) = \frac{n-(x-1)}{n}\bigg(E(x-1)\sum_{i=1}^{\infty}\bigg( \frac{x-1}{n} \bigg)^{i-1} + \sum_{i=1}^{\infty}i*\bigg( \frac{x-1}{n} \bigg)^{i-1}\bigg)
E(x) = \frac{n-(x-1)}{n}\bigg(E(x-1)*\frac{n}{n-(x-1)} + \frac{1}{(1 - \frac{x-1}{n})^2} \bigg)
E(x) = E(x-1) + \frac{n}{n-(x-1)}

Now E(0) = 0 so ans would be

\sum_{x=1}^n \frac{n}{n-(x-1)} = n * \sum_{i=1}^n \frac{1}{i}

Hope this clears your doubt.

1 Like

@vijju123 can u please help me!

Probabilities arent actually my thing yet…Sorry but I am weaker than you in this field XD

@Pk301 bro you started coding a month after I started but now you can solve medium-based problems!! I still can only do easy problems. Any tips for me??

@kunnu120 i have just started with medium you can also start by sorting the problems and if u have any doubts feel free to ask :slight_smile: always ready to help !

1 Like

@vijju123 thanks for your concern :slight_smile:

thanks for this but i had already read the editorial and only want to know where i am thinking wrong!

For two attempts, shouldnt the probability be :


As in first attempt,N-1 songs can be picked and in 2nd 1 song

i am considering only one song at a time and for the remaining songs i just use the previous answer and so i thought it would be (1/n^2)!
but i know my thinking is wrong :frowning:
Thanks for looking into it :slight_smile: if possible please spent a little more time on this and please figure out mistake !


Use latex please. Just put the formulas between $ signs.

1 Like

And please format your post next time. break it into paragraphs and learn basic latex. It’s not hard.

1 Like

thanks for this :slight_smile:
in my approach E(x) denotes that what will be the expected number of times we listen so as to complete list of x songs. That’s why i am focusing on any 1 song (to start with and in the go if that song is repeated it’s prob is (1/n) ).
But now i understood doing this didn’t make any sense as probability in previous calculation would not be the same!
thanks again ! made my day :slight_smile:

i will surely learn latex and next time it won’t be the case :slight_smile:

No problem mate. I also thought that at first and arrived at E(x) = E(x-1) + n and since this was clearly wrong for x=3 I realized my mistake.
So even if you thought like that your calculation was off(that \frac{1}{n^2}) and you are right to think to multiply by n but you also had to divide it by n, since you want the average.