Program to calculate expected increase in price P after N consecutive days
Last Updated :
15 Dec, 2022
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.
Examples:
Input: P = 1000, A = 5, B = 10, N = 10
Output: 1075
Explanation:
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
Output: 2450
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:
C++
#include <bits/stdc++.h>
using namespace std;
void expectedValue( int P, int a,
int b, int N)
{
double expValue
= P + (N * 0.5 * (a + b));
cout << expValue;
}
int main()
{
int P = 3000, a = 20, b = 10, N = 30;
expectedValue(P, a, b, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void expectedValue( int P, int a,
int b, int N)
{
double expValue = P + (N * 0.5 * (a + b));
System.out.print(expValue);
}
public static void main(String[] args)
{
int P = 3000 , a = 20 , b = 10 , N = 30 ;
expectedValue(P, a, b, N);
}
}
|
Python3
def expectedValue(P, a, b, N):
expValue = P + (N * 0.5 * (a + b))
print ( int (expValue))
if __name__ = = '__main__' :
P = 3000
a = 20
b = 10
N = 30
expectedValue(P, a, b, N)
|
C#
using System;
class GFG{
static void expectedValue( int P, int a,
int b, int N)
{
double expValue = P + (N * 0.5 * (a + b));
Console.Write(expValue);
}
static void Main()
{
int P = 3000, a = 20, b = 10, N = 30;
expectedValue(P, a, b, N);
}
}
|
Javascript
<script>
function expectedValue(P, a, b, N)
{
var expValue = P + (N * 0.5 * (a + b)) ;
return expValue;
}
var P = 3000
var a = 20
var b = 10
var N = 30
document.write(expectedValue(P, a, b, N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...