Program to calculate expected increase in price P after N consecutive days
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)