Link to problem:

I keep getting 0.5 as the solution for any input when I try to solve on paper.

However, the answer appears to be (q+1)/(p+2). You can try various inputs here:

http://www.spojtoolkit.com/test/PRLGAME2

For example, say n=3, p=1, q=1. According to the solution, answer should be 2/3. My working is below. Can somebody help me as to where I am going wrong?

p=1, q=1, meaning now there are 2 balls left in the bag, and the only ball which was taken out was red. This means originally in the bag, there must have been 1, 2 or 3 red balls (0 red balls are not possible as we have taken out 1, meaning there were a minimum of 1 red balls).

So probability that the next ball is red =

1/3 * 0 (case where there were originally only 1 red ball in the bag, which got taken out, hence no more red balls left) +

1/3 * 1/2 (originally 2 red balls, hence only 1 red ball left in the bag out of 2 balls) + 1/3 * 2/2

This gives the answer as 1/2. The generic formula I got was also along these lines.

EDIT - Bumping this up. Some help here would be awesome.