Computer Science, asked by radorexx1, 10 months ago

Stock span
The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate the span of stock’s price for all n days. The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day. Now, you need to find out the span values for the given number of days and their daily prices. For example, if an array of 7 day's prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6} . FUNCTIONAL REQUIREMENTS: void stockSpan(int,int*);

Answers

Answered by Elliot8
7

Answer:

#include<iostream>

using namespace std;

void stockSpan(int n,int *a)

{

 int b[n],count=0;

 for(int i=0;i<n;i++)

 {b[i]=0;

   for(int j=0;j<=i;j++)

   {

     if(a[j]<=a[i])

       b[i]++;

     else

       b[i]=0;

   }

 }

 for(int i=0;i<n;i++)

   cout<<b[i]<<endl;

}

int main()

{

 //Type your code here.

 int n;

 cin>>n;

 int a[n];

 for(int i=0;i<n;i++)

   cin>>a[i];

 stockSpan(n,a);

}

Explanation:

Similar questions