The cost of stock on each day is given in an array price[] of size n. Find all the days on which you buy and sell the stock so that in between those days your profit is maximum.
Note: 
The output format is as follows - (buy_day sell_day) (buy_day sell_day)
For each input, the output should be in a single line, i.e. It's important to move to a new/next line for printing the output of other test cases.
Example 1:
Input:
n = 7
price[] = { 100, 180, 260, 310, 40, 535, 695 }
Output:
(0 3) (4 6)
Explanation:
We can buy stock on day 1 and sell it on day 4, 
We can buy another stock on day 5 and sell it on day 7, 
which will give us maximum profit.
Example 2:
Input:
n = 10
price[] = {23, 13, 25, 29, 33, 19, 34, 45, 65, 67}
Output:
(1 4) (5 9)
Explanation:
We can buy stock on day 1 and sell it on day 4, 
We can buy another stock on day 5 and sell it on day 9, 
which will give us maximum profit.
Your Task:
Complete stockBuySell()  function and print all the days with profit in a single line. And if there is no profit then print "No Profit". You do not require to return since the function is void.
Expected Time Complexity: O(n)Expected Auxiliary Space: O(n)
Constraints:
2 <= n <= 10^{4}
0 <= price[i] <= 10^{4}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Data structures

Starter code: #User function Template for python3







def stockBuySell(price, n):

    # code here

Solutions:
def stockBuySell(p, n):
	start = 0
	i = 1
	ans = []
	while i < n:
		if p[i - 1] < p[i]:
			i += 1
		else:
			if start != i - 1:
				ans.append([start, i - 1])
			start = i
			i += 1
	if start != n - 1:
		ans.append([start, n - 1])
	if not ans:
		print('No Profit')
		return
	else:
		for i in ans:
			print('(' + str(i[0]) + ' ' + str(i[1]) + ')', end=' ')
		print()


from collections import deque

def stockBuySell(price, n):
	a = price
	buy = 0
	sell = 0
	i = 1
	notPresent = True
	while i < n:
		buy = i - 1
		while i < n and a[i] > a[i - 1]:
			i += 1
		sell = i - 1
		if buy != sell:
			notPresent = False
			print('({} {})'.format(buy, sell), end=' ')
		i += 1
	if notPresent:
		print('No Profit', end=' ')
	print()


def stockBuySell(price, n):
	start = 0
	i = 1
	ans = []
	flag = 0
	for i in range(n):
		if price[i - 1] < price[i]:
			continue
		else:
			if start < i - 1:
				ans.append([start, i - 1])
				flag = 1
			start = i
	if start < n - 1:
		ans.append([start, n - 1])
		flag = 1
	if not flag:
		print('No Profit')
	else:
		for i in ans:
			print('({0} {1})'.format(i[0], i[1]), end=' ')
		print()


def stockBuySell(price, n):
	start = 0
	i = 1
	ans = []
	while i < n:
		if price[i - 1] < price[i]:
			i += 1
		else:
			if start != i - 1:
				ans.append([start, i - 1])
			start = i
			i += 1
	if start != n - 1:
		ans.append([start, n - 1])
	if not ans:
		print('No Profit')
		return
	else:
		for i in ans:
			print('({0} {1})'.format(i[0], i[1]), end=' ')
		print()


def stockBuySell(a, n):
	buy = 0
	sell = 0
	i = 1
	notPresent = True
	while i < n:
		buy = i - 1
		while i < n and a[i] > a[i - 1]:
			i += 1
		sell = i - 1
		if buy != sell:
			notPresent = False
			print('({} {})'.format(buy, sell), end=' ')
		i += 1
	if notPresent:
		print('No Profit', end=' ')
	print()


def stockBuySell(price, n):
	if n == 1:
		print('No Profit')
	res = []
	(currIdx, currPrice) = (0, price[0])
	for i in range(1, n):
		if price[i] >= price[i - 1]:
			continue
		else:
			if price[i - 1] > currPrice:
				res.append('({} {})'.format(currIdx, i - 1))
			(currIdx, currPrice) = (i, price[i])
	if price[n - 1] > currPrice:
		res.append('({} {})'.format(currIdx, n - 1))
	if res:
		print(' '.join(res))
	else:
		print('No Profit')


def stockBuySell(price, n):
	if n == 1:
		return
	flag = False
	i = 0
	while i < n - 1:
		while i < n - 1 and price[i + 1] <= price[i]:
			i += 1
		if i == n - 1:
			break
		buy = i
		i += 1
		while i < n and price[i] >= price[i - 1]:
			i += 1
		sell = i - 1
		print('(' + str(buy) + ' ' + str(sell) + ')', end=' ')
		flag = True
	if flag == False:
		print('No Profit', end='')
	print()


def stockBuySell(price, n):
	j = 0
	m = price[0]
	flag = True
	for i in range(1, n - 1):
		if price[i] <= m:
			m = price[i]
			j = i
		elif price[i + 1] < price[i]:
			print('(' + str(j) + ' ' + str(i) + ')', end=' ')
			j = i + 1
			m = price[i + 1]
			flag = False
	if price[-1] > m:
		print('(' + str(j) + ' ' + str(n - 1) + ')')
		flag = False
	elif flag:
		print('No Profit')
	else:
		print()


def stockBuySell(price, n):
	low = 0
	rise = 0
	val = price[rise]
	ans = []
	while rise < n:
		if val <= price[rise]:
			val = price[rise]
			rise += 1
		else:
			if low < rise - 1:
				ans.append([low, rise - 1])
			low = rise
			rise = rise + 1
			val = price[low]
	if rise - 1 > low:
		ans.append([low, rise - 1])
	if ans:
		for i in range(len(ans)):
			print('(' + str(ans[i][0]) + ' ' + str(ans[i][1]) + ')', end=' ')
		print()
	else:
		print('No Profit')


def stockBuySell(price, n):
	(start, end, no_profit) = (0, 0, True)
	for j in range(0, n - 1):
		if price[j] > price[j + 1]:
			end = j
			if start != end:
				print('(' + str(start) + ' ' + str(end) + ')', end=' ')
				no_profit = False
			start = j + 1
	if price[n - 2] < price[n - 1]:
		end = n - 1
		print('(' + str(start) + ' ' + str(end) + ')', end=' ')
		no_profit = False
	if no_profit == True:
		print('No Profit', end=' ')
	print()


def stockBuySell(price, n):
	is_profitable = False
	trading_day = 0
	while trading_day < n - 1:
		while trading_day < n - 1 and price[trading_day] >= price[trading_day + 1]:
			trading_day += 1
		if trading_day >= n - 1:
			break
		buy_day = trading_day
		trading_day += 1
		while trading_day < n and price[trading_day] >= price[trading_day - 1]:
			trading_day += 1
		sell_day = trading_day - 1
		print('({0} {1})'.format(buy_day, sell_day), end=' ')
		is_profitable = True
	if not is_profitable:
		print('No Profit', end='')
	print()


def stockBuySell(price, n):
	buy_sell_deals = []
	trading_day = 0
	while trading_day < n - 1:
		while not (trading_day == 0 and price[trading_day] < price[trading_day + 1] or (trading_day == n - 1 and price[trading_day - 1] > price[trading_day]) or (price[trading_day - 1] > price[trading_day] and price[trading_day] < price[trading_day + 1])):
			trading_day += 1
		last_local_minimum_day = trading_day
		if trading_day >= n - 1:
			break
		while not (trading_day == 0 and price[trading_day] > price[trading_day + 1] or (trading_day == n - 1 and price[trading_day - 1] < price[trading_day]) or (price[trading_day - 1] < price[trading_day] and price[trading_day] > price[trading_day + 1])):
			trading_day += 1
		last_local_maximum_day = trading_day
		delta = price[last_local_maximum_day] - price[last_local_minimum_day]
		if delta > 0:
			buy_sell_deals.append((last_local_minimum_day, last_local_maximum_day))
		trading_day += 1
	if buy_sell_deals:
		print(' '.join(map(lambda t: '({0} {1})'.format(t[0], t[1]), buy_sell_deals)))
	else:
		print('No Profit')


def stockBuySell(price, n):
	(buy, sell, f) = (-1, -1, 0)
	for i in range(n - 1):
		if price[i] < price[i + 1]:
			if buy == -1:
				buy = i
		elif buy != -1:
			sell = i
			f = 1
			print('(', buy, ' ', sell, ')', sep='', end=' ')
			buy = -1
		if i == n - 2 and buy != -1:
			f = 1
			print('(', buy, ' ', i + 1, ')', sep='', end=' ')
	if f == 0:
		print('No Profit')
	else:
		print()