The cost of stock on each day is given in an array A[] of size N. Find all the segments of days on which you buy and sell the stock so that in between those days for which profit can be generated.
Note: Since there can be multiple solutions, the driver code will print 1 if your answer is correct, otherwise, it will return 0. In case there's no profit the driver code will print the string "No Profit" for a correct solution.
Example 1:
Input:
N = 7
A[] = {100,180,260,310,40,535,695}
Output:
1
Explanation:
One possible solution is (0 3) (4 6)
We can buy stock on day 0,
and sell it on 3rd day, which will 
give us maximum profit. Now, we buy 
stock on day 4 and sell it on day 6.
Example 2:
Input:
N = 5
A[] = {4,2,2,2,4}
Output:
1
Explanation:
There are multiple possible solutions.
one of them is (3 4)
We can buy stock on day 3,
and sell it on 4th day, which will 
give us maximum profit.
Your Task:
The task is to complete the function stockBuySell() which takes an array of A[] and N as input parameters and finds the days of buying and selling stock. The function must return a 2D list of integers containing all the buy-sell pairs i.e. the first value of the pair will represent the day on which you buy the stock and the second value represent the day on which you sell that stock. If there is No Profit, return an empty list. 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
2 ≤ N ≤ 10^{6}
0 ≤ A[i] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Dynamic programming

Data structures

Greedy algorithms

Starter code: #User function template for Python



class Solution:

    #Function to find the days of buying and selling stock for max profit.

	def stockBuySell(self, A, n):

		#code here

Solutions:
a = []

def days_store(A, x, n):
	min = 0
	for i in range(x, n - 1):
		if A[i + 1] > A[i]:
			a.append(i)
			min = A[i + 1]
			break
	for j in range(i + 1, n):
		if min > A[j]:
			a.append(j - 1)
			return j
			break
		elif j == n - 1:
			a.append(n - 1)
			return j
		min = A[j]

class Solution:

	def stockBuySell(self, A, n):
		ans = []
		for i in range(n - 1):
			if A[i] < A[i + 1]:
				ans.append((i, i + 1))
		return ans


class Solution:

	def stockBuySell(self, A, n):
		l = []
		for i in range(1, n):
			if A[i] > A[i - 1]:
				l.append([i - 1, i])
		return l


class Solution:

	def stockBuySell(self, A, n):
		if n == 0:
			return []
		lis = []
		for i in range(n - 1):
			if A[i] < A[i + 1]:
				lis.append((i, i + 1))
		return lis


class Solution:

	def stockBuySell(self, A, n):
		s = []
		buy = 0
		sell = 0
		for i in range(1, n):
			l = []
			if A[i] > A[sell]:
				sell = i
			else:
				if sell > buy:
					l.append(buy)
					l.append(sell)
					s.append(l)
				buy = i
				sell = i
		l = []
		if sell > buy:
			l.append(buy)
			l.append(sell)
			s.append(l)
		return s


class Solution:

	def stockBuySell(self, A, n):
		ans = []
		t = [0]
		for i in range(n - 1):
			if A[i] > A[i + 1]:
				if i != t[0]:
					t.append(i)
					ans.append(t)
					t = [i + 1]
				else:
					t = [i + 1]
		if len(t) == 1:
			if A[t[0]] < A[-1]:
				t.append(n - 1)
				ans.append(t)
		return ans


class Solution:

	def stockBuySell(self, A, N):
		if N < 2:
			return []
		buy_sell_pairs = []
		i = 0
		while i < N - 1:
			while i < N - 1 and A[i] >= A[i + 1]:
				i += 1
			if i == N - 1:
				break
			buy = i
			while i < N - 1 and A[i] <= A[i + 1]:
				i += 1
			sell = i
			buy_sell_pairs.append((buy, sell))
		return buy_sell_pairs


class Solution:

	def stockBuySell(self, A, n):
		local_min = 0
		local_max = 0
		res = []
		for i in range(1, n):
			if A[local_max] > A[i] and local_min < local_max:
				res.append((local_min, local_max))
				local_min = i
				local_max = i
			if A[local_min] > A[i]:
				local_min = i
				local_max = i
			if A[local_max] < A[i]:
				local_max = i
		if local_min < local_max:
			res.append((local_min, local_max))
		return res


class Solution:

	def stockBuySell(self, a, n):
		res = []
		i = 0
		for i in range(n - 1):
			if a[i + 1] > a[i]:
				res.append([i, i + 1])
		return res


class Solution:

	def stockBuySell(self, A, n):
		i = 0
		j = 1
		l = []
		while j < len(A):
			if A[i] < A[j]:
				l.append((i, j))
				i = i + 1
			elif A[i] >= A[j]:
				i = i + 1
			j = j + 1
		return l


class Solution:

	def stockBuySell(self, A, n):
		buy = 0
		sel = 1
		ans = []
		while buy < n and sel < n:
			if A[buy] < A[sel]:
				ans.append([buy, sel])
				buy += 1
				sel += 1
			else:
				buy = sel
				sel += 1
		return ans


class Solution:

	def stockBuySell(self, A, n):
		profit = []
		for i in range(n - 1):
			if A[i] < A[i + 1]:
				profit.append((i, i + 1))
		if not profit:
			return []
		return profit


class Solution:

	def stockBuySell(self, A, n):
		if n < 2:
			return []
		sell = 0
		buy = 0
		buy_sell_pair = []
		for i in range(1, n):
			if A[i] > A[i - 1]:
				sell = i
			else:
				if sell > buy:
					buy_sell_pair.append((buy, sell))
				buy = i
		if sell > buy:
			buy_sell_pair.append((buy, sell))
		if not buy_sell_pair:
			return []
		return buy_sell_pair


class Solution:

	def stockBuySell(self, A, n):
		if n == 1:
			return []
		i = 0
		lis = []
		while i < n - 1:
			if A[i] < A[i + 1]:
				lis.append((i, i + 1))
			i += 1
		return lis


class Solution:

	def stockBuySell(self, A, N):
		if N == 1:
			return []
		i = 0
		profit = []
		while i < N - 1:
			if A[i] < A[i + 1]:
				profit.append((i, i + 1))
			i += 1
		return profit


class Solution:

	def stockBuySell(self, A, n):
		if n < 2:
			return []
		buy_sell_pairs = []
		buy_day = 0
		for i in range(1, n):
			if A[i] < A[i - 1]:
				if i - 1 > buy_day:
					sell_day = i - 1
					buy_sell_pairs.append([buy_day, sell_day])
				buy_day = i
		if n - 1 > buy_day:
			sell_day = n - 1
			buy_sell_pairs.append([buy_day, sell_day])
		return buy_sell_pairs


class Solution:

	def stockBuySell(self, A, n):
		r = []
		i = 0
		j = 1
		n = len(A)
		while i < n - 1:
			if A[j] <= A[i]:
				i += 1
				j += 1
				if j == n:
					break
			else:
				while A[j] > A[j - 1] and j < n:
					j += 1
					if j == n:
						break
				r.append((i, j - 1))
				i = j
				j += 1
		return list(set(r))


class Solution:

	def stockBuySell(self, A, n):
		if n <= 1:
			return []
		buy_sell = []
		buy = 0
		sell = 0
		for i in range(1, n):
			if A[i] > A[i - 1]:
				sell = i
			elif A[i] <= A[i - 1]:
				if buy != sell:
					buy_sell.append((buy, sell))
				buy = sell = i
		if buy != sell:
			buy_sell.append((buy, sell))
		return buy_sell


class Solution:

	def stockBuySell(self, A, n):
		l = []
		buy = 0
		sell = 0
		i = 0
		while i < n - 1:
			while i < n - 1 and A[i + 1] <= A[i]:
				i += 1
			if i == n - 1:
				break
			buy = i
			i += 1
			while i < n and A[i] >= A[i - 1]:
				i += 1
			sell = i - 1
			l.append((buy, sell))
		return l


class Solution:

	def stockBuySell(self, prices, n):
		buy_sell_days = []
		i = 0
		while i < n - 1:
			while i < n - 1 and prices[i + 1] <= prices[i]:
				i += 1
			if i == n - 1:
				break
			buy_day = i
			i += 1
			while i < n and prices[i] >= prices[i - 1]:
				i += 1
			sell_day = i - 1
			buy_sell_days.append((buy_day, sell_day))
		return buy_sell_days


class Solution:

	def stockBuySell(self, A, n):
		segs = []
		cur_start = 0
		cur_end = 0
		prev = A[0]
		for a in range(1, len(A)):
			if A[a] < prev:
				if cur_end - cur_start > 0:
					segs.append([cur_start, cur_end])
				cur_start = a
			elif a == len(A) - 1:
				segs.append([cur_start, a])
			else:
				cur_end = a
			prev = A[a]
		return segs


class Solution:

	def stockBuySell(self, A, n):
		result = []
		start = 0
		for i in range(n):
			if A[i] <= A[start]:
				start = i
			else:
				result.append([start, i])
				start = i
		return result


class Solution:

	def stockBuySell(self, arr, n):
		ans = []
		for i in range(n - 1):
			if arr[i] < arr[i + 1]:
				ans.append((i, i + 1))
		return ans


class Solution:

	def stockBuySell(self, A, n):
		diff = 0
		s = []
		low = 0
		for i in range(1, n):
			diff = A[i] - A[i - 1]
			if diff < 0:
				if i - 1 > low:
					s.append([low, i - 1])
				low = i
		if low < n - 1:
			s.append([low, n - 1])
			low = i
		return s


class Solution:

	def stockBuySell(self, a, n):
		r = []
		buy = 0
		if n == 2:
			if a[0] >= a[1]:
				return []
			else:
				return [[0, 1]]
		for i in range(1, n):
			if a[i - 1] > a[i]:
				if a[i - 1] > a[buy]:
					r.append([buy, i - 1])
				buy = i
			elif i == n - 1 and a[i] > a[buy]:
				r.append([buy, i])
		return r


class Solution:

	def stockBuySell(self, A, n):
		curmin = 0
		curmax = None
		i = 1
		result = []
		while i < n:
			if curmax == None:
				if A[i] > A[curmin]:
					curmax = i
				else:
					curmin = i
			elif A[i] > A[curmax]:
				curmax = i
			else:
				result.append([curmin, curmax])
				curmin = i
				curmax = None
			i += 1
		if curmax is not None:
			result.append([curmin, curmax])
		return result


class Solution:

	def stockBuySell(self, A, n):
		i = 1
		ans = []
		start = 0
		while i < n:
			if A[i] < A[i - 1]:
				if i - 1 != start:
					ans.append([start, i - 1])
				start = i
			i += 1
		if i - 1 != start:
			ans.append([start, i - 1])
		return ans


class Solution:

	def stockBuySell(self, A, n):
		res = []
		for i in range(0, len(A) - 1):
			if A[i + 1] > A[i]:
				res.append([i, i + 1])
		return res


class Solution:

	def stockBuySell(self, A, n):
		refresher = max(A)
		buy = refresher
		pair = list()
		s = 0
		b = 0
		buys = 0
		sell = 0
		for i in range(n - 1):
			if A[i] < A[i + 1]:
				if buy >= A[i]:
					buys = i
					b += 1
					buy = A[i]
				else:
					continue
			elif b == s + 1:
				sell = i
				buy = refresher
				s += 1
				pair.append([buys, sell])
			else:
				continue
		if A[n - 1] > A[n - 2]:
			sell = n - 1
			pair.append([buys, sell])
		return pair


class Solution:

	def stockBuySell(self, A, n):
		profit = []
		i = 1
		while i < n:
			if A[i - 1] < A[i]:
				profit += [[i - 1, i]]
			i += 1
		return profit


class Solution:

	def stockBuySell(self, A, N):
		result = []
		maxval = 0
		for i in range(1, N):
			if A[i] > A[i - 1]:
				result.append((i - 1, i))
		return result


class Solution:

	def stockBuySell(self, A, n):
		ans = []
		i = n - 1
		while i >= 0:
			last = i
			while i - 1 >= 0 and A[i] > A[i - 1]:
				i -= 1
			if last != i:
				ans.append((i, last))
			i -= 1
		return ans


class Solution:

	def stockBuySell(self, A, n):
		an = []
		a = A
		x = 0
		n = len(A)
		while x < n - 1:
			i = x
			s = x
			while a[i + 1] > a[i]:
				i += 1
				x += 1
				if i == n - 1:
					break
			f = [s, i]
			if f[0] != f[1]:
				an.append(f)
			x += 1
		return an


class Solution:

	def stockBuySell(self, A, n):
		result = []
		if n == 0 or n == 1:
			return result
		B = 0
		S = B
		while S < n - 1:
			if A[S] <= A[S + 1]:
				S = S + 1
			else:
				if B != S:
					result.append([B, S])
				B = S + 1
				S = B
		if B != S:
			result.append([B, S])
		return result


class Solution:

	def stockBuySell(self, a, n):
		j = 1
		n = len(a)
		pairs = []
		if n == 1:
			return pairs
		flag = 0
		buy = -1
		sell = -1
		for i in range(n):
			if flag == 0:
				if a[i] >= a[j]:
					if j != n - 1:
						j = j + 1
				else:
					buy = i
					flag = 1
					if j != n - 1:
						j = j + 1
			elif a[i] < a[j]:
				if j == n - 1:
					flag = 0
					sell = i + 1
				else:
					j = j + 1
			else:
				flag = 0
				sell = i
				if j != n - 1:
					j = j + 1
			if buy != -1 and sell != -1:
				pairs.append((buy, sell))
				buy = -1
				sell = -1
		return pairs


class Solution:

	def stockBuySell(self, A, n):
		i = 0
		ans = []
		b = 0
		s = 0
		while i < n:
			while i < n - 1 and A[i + 1] < A[i]:
				i += 1
			if i == n - 1:
				break
			b = i
			i = i + 1
			while i < n and A[i - 1] < A[i]:
				i += 1
			s = i - 1
			ans.append([b, s])
		return ans


class Solution:

	def stockBuySell(self, arr, n):
		buy = A[0]
		buydate = 0
		list = []
		temp = len(A)
		for i in range(1, temp):
			if buy < A[i]:
				list.append([buydate, i])
				buy = A[i]
				buydate = i
			else:
				buy = A[i]
				buydate = i
		return list


class Solution:

	def stockBuySell(self, A, n):
		out = []
		price = A
		buy = 0
		sell = 0
		profit = 0
		for i in range(1, n):
			if price[i] >= price[i - 1]:
				sell += 1
			else:
				profit += price[sell] - price[buy]
				if buy != sell:
					out.append([buy, sell])
				buy = i
				sell = i
		if buy != sell:
			out.append([buy, sell])
		return out


class Solution:

	def stockBuySell(self, A, n):
		l = []
		s = 0
		e = 1
		while e < n:
			if A[e - 1] < A[e]:
				e = e + 1
			else:
				if s != e - 1:
					l.append([s, e - 1])
				s = e
				e = e + 1
		if s == 0 and e == n and (A[s] >= A[e - 1]):
			return l
		if s != e - 1:
			l.append([s, e - 1])
		return l


class Solution:

	def stockBuySell(self, arr, n):
		ans = []
		buy = 0
		sell = 0
		for i in range(n - 1):
			if arr[i + 1] > arr[i]:
				sell += 1
			else:
				if sell > buy:
					li = []
					li.append(buy)
					li.append(sell)
					ans.append(li)
				sell = i + 1
				buy = i + 1
		if sell > buy:
			li = []
			li.append(buy)
			li.append(sell)
			ans.append(li)
		return ans


class Solution:

	def stockBuySell(self, A, n):
		buy_sell_pairs = []
		buy = sell = -1
		for i in range(n - 1):
			if A[i] < A[i + 1]:
				if buy == -1:
					buy = i
			elif buy != -1 and A[i] > A[buy]:
				sell = i
				buy_sell_pairs.append([buy, sell])
				buy = sell = -1
		if buy != -1:
			sell = n - 1
			buy_sell_pairs.append([buy, sell])
		return buy_sell_pairs


class Solution:

	def stockBuySell(self, a, n):
		i = 0
		ans = []
		while i < n:
			while i + 1 < n and a[i] >= a[i + 1]:
				i += 1
			l = i
			while i + 1 < n and a[i] <= a[i + 1]:
				i += 1
			r = i
			if l == r:
				return ans
			else:
				ans.append([l, r])
				i += 1
		return ans