Given an array which contains stock price on each day, find the maximum profit that you can make by buying and selling in those days. Here, we can buy and sell multiple times but only after selling a stock you can buy another stock.

Example

INPUT :arr[] = {4, 9, 7, 15, 20}

OUTPUT :Buy on day 0 and sell on day 1Buy on day 2 and sell on day 4.

Time Complexity: O(n)

Algorithm

Initialize count = 0, ie number of buy sell pairsTill the end of the array

1. find the local minima and store it as the starting index ie, buying on that day

2. find local maxima and store it as ending index ie, selling on that day