5 - Stock Market

3.2

4 votes
Problem

Mr. Mallya a business tycoon of Bangalore went on a vacation to Vegas. Being an alcoholic he gambled senselessly a large amount of his property in the casino. He was out of luck. He had to suffer a huge loss. After returning from there he decided to heal his losses. He consulted his son studying Business Management in Harvard University, USA. His son, after scrutinizing the stock market conditions in India advised his father to invest in the following manner so as to make the maximum profit:

The cost of a stock on each day is given in an array, find the maximum profit that you can make by buying and selling in those days.

For example, if the given array is {100, 180, 260, 310, 40, 535, 695} , the maximum profit can earned by buying on day 0, selling on day 3. Again buy on day 4 and sell on day 6. If the given array of prices is sorted in decreasing order, then profit cannot be earned at all.

Input:

The first line contains ‘n’ i.e. no. of elements. (1 ≤ n ≤ 100)

The second line contains the respective n elements of the array i.e cost of stock in nth day (separated by a space)

Output:

Each line contains the respective buying and selling day so as to make maximum profit.

Sample Input

7

100 180 260 310 40 535 695

Sample Output

0 3

4 6

(Explanation: The first line outputs the buying day as 0th day and selling day as 3rd day. Similarly next line outputs the buying day as 4th day and selling day as 6th day. In this way he makes maximum profit.)

Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?