Open In App

Program to calculate expected increase in price P after N consecutive days

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:

  1. 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.
  2. Therefore, increase in value after one day = (a + b) / 2.
  3. Therefore, increase in value after N days = N * (a + b) / 2.
  4. Therefore, increased value after N days = P + N * (a + b) / 2.
  5. Print the increased value as the required answer.

Below is the implementation of the above approach:

C++




// C++ program for
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the increased
// value of P after N days
void expectedValue(int P, int a,
                   int b, int N)
{
    // Expected value of the
    // number P after N days
    double expValue
        = P + (N * 0.5 * (a + b));
 
    // Print the expected value
    cout << expValue;
}
 
// Driver Code
int main()
{
    int P = 3000, a = 20, b = 10, N = 30;
    expectedValue(P, a, b, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
class GFG{
 
// Function to find the increased
// value of P after N days
static void expectedValue(int P, int a,
                          int b, int N)
{
     
    // Expected value of the
    // number P after N days
    double expValue = P + (N * 0.5 * (a + b));
 
    // Print the expected value
    System.out.print(expValue);
}
 
// Driver code
public static void main(String[] args)
{
    int P = 3000, a = 20, b = 10, N = 30;
    expectedValue(P, a, b, N);
}
}
 
// This code is contributed by abhinavjain194


Python3




# Python3 program for
# the above approach
 
# Function to find the increased
# value of P after N days
def expectedValue(P, a, b, N):
     
    # Expected value of the
    # number P after N days
    expValue = P + (N * 0.5 * (a + b))
 
    # Print the expected value
    print(int(expValue))
 
# Driver Code
if __name__ == '__main__':
     
    P = 3000
    a = 20
    b = 10
    N = 30
     
    expectedValue(P, a, b, N)
     
# This code is contributed by ipg2016107


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the increased
// value of P after N days
static void expectedValue(int P, int a,
                          int b, int N)
{
     
    // Expected value of the
    // number P after N days
    double expValue = P + (N * 0.5 * (a + b));
 
    // Print the expected value
    Console.Write(expValue);
}
 
// Driver code
static void Main()
{
    int P = 3000, a = 20, b = 10, N = 30;
    expectedValue(P, a, b, N);
}
}
 
// This code is contributed by abhinavjain194


Javascript




<script>
 
// Javascript program for
// the above approach
  
// Function to find the increased
// value of P after N days
 
    function expectedValue(P, a, b, N)
    {
     
       // Expected value of the
       // number P after N day
     
       var expValue = P + (N * 0.5 * (a + b)) ;
        
       return expValue;
      }
  
    // Driver code
     
      var P = 3000
      var a = 20
      var b = 10
      var N = 30
  
     document.write(expectedValue(P, a, b, N));
  
</script>


Output: 

3450

 

Time Complexity: O(1)
Auxiliary Space: O(1)

 



Last Updated : 15 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads