Computer Science, asked by lakshmisravani7119, 6 months ago

You are given an array containing the prices of a painting on 7
consecutive days (Day1, Day2, etc). You have to buy
the painting on
some day (say Day3) and sell it at some later day (say Day5). Write a
program which
will calculate the maximum possible profit you can
make and the days on which the painting should be bought and sold.
Example Output:
• If the given array is {100, 180, 260, 310, 40, 535, 695), maximum
profit of 655 can be earned by buying on Day5 and selling on
Day7
• Output Format: Buy on - Day 5 - Sell on - Day 7 - Profit = 655​

Answers

Answered by dreamrob
39

#include<iostream>

using namespace std;

int main()

{

int price[7];

cout<<"Enter price from Day1 to Day7"<<endl;

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

{

 cin>>price[i];

}

int max=-99999,buy,sold;

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

{

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

 {

  if((price[j]-price[i])>max)

  {

   max=price[j]-price[i];

   buy=i;

   sold=j;

  }

 }

}

cout<<"Buy on - Day"<<buy+1<<endl;

cout<<"Sell on - Day"<<sold+1<<endl;

cout<<"Profit = "<<max;

}

Similar questions