Given an integer P, that increases either by A or B with 50% probability each, in the next N consecutive days, the task is to find the expected value after N days.
Input: P = 1000, A = 5, B = 10, N = 10
Expected increased value after N consecutive days is equal to:
P + N * (a + b) / 2 = 1000 + 10 × 7.5 = 1000 + 75 = 1075.
Input: P = 2000, a = 10, b = 20, N = 30
Approach: Follow the steps to solve the problem:
- Expected value of increase each day = (Probability of increase by A) * A + (Probability of value increase by B) * B = (1 / 2) * A + (1 / 2) * B.
- Therefore, increase in value after one day = (a + b) / 2.
- Therefore, increase in value after N days = N * (a + b) / 2.
- Therefore, increased value after N days = P + N * (a + b) / 2.
- Print the increased value as the required answer.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.