There are N coins placed on the road where arr[i] is the denomination of i^{th} coin. A Fox is looking to collect some of these coins. The fox is very greedy and follows a weird pattern of collecting coins: the fox collects coins in only increasing order of their denomination since it always craves for more money and all the coins it collects are contiguous. The fox wants to maximize the amount of money it collects.
Unfortunately, the Fox is greedy but not intelligent enough to find the solution and asks you for help. Find the maximum amount of money the fox can collect. 
 
Example 1:
Input:
N=6
arr[] = {2, 1, 4, 7, 3, 6} 
Output: 12
Explanation: Contiguous Increasing subarray 
             {1, 4, 7} = 12.
 
Example 2:
Input:
N=5
arr[] = {38, 7, 8, 10, 12} 
Output: 38
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function largestSum() that takes array arr and integer N as parameters and returns the desired output.
 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3





def largestSum(arr, N):



Solutions:
def largestSum(arr, N):
	current = 0
	mx_ = 0
	arr.append(float('inf'))
	for index in range(N):
		current += arr[index]
		if arr[index] >= arr[index + 1]:
			mx_ = mx_ if mx_ > current else current
			current = 0
	mx_ = mx_ if mx_ > current else current
	return mx_


def largestSum(arr, N):
	ma = 0
	i = 0
	while i < N:
		cur = arr[i]
		i += 1
		while i < N and arr[i] > arr[i - 1]:
			cur += arr[i]
			i += 1
		ma = max(cur, ma)
	return ma


def largestSum(arr, N):
	i = 0
	summ = arr[0]
	ans = -10000007
	for i in range(1, N):
		if arr[i - 1] < arr[i]:
			summ += arr[i]
		else:
			ans = max(ans, summ)
			summ = arr[i]
	return max(ans, summ)


def largestSum(arr, N):
	result = 0
	for i in range(N):
		curr = arr[i]
		while i + 1 < N and arr[i + 1] > arr[i]:
			curr += arr[i + 1]
			i += 1
		if curr > result or result is None:
			result = curr
	return result


def largestSum(arr, N):
	summa = arr[0]
	maxi = summa
	for i in range(N - 1):
		if arr[i] < arr[i + 1]:
			summa += arr[i + 1]
		else:
			summa = arr[i + 1]
		maxi = max(maxi, summa)
	return maxi


def largestSum(arr, N):
	ans = 0
	for i in range(N):
		summ = arr[i]
		while i + 1 < N and arr[i + 1] > arr[i]:
			summ += arr[i + 1]
			i += 1
		if summ > ans:
			ans = summ
	return ans


def largestSum(arr, N):
	m = arr[0]
	s = arr[0]
	for i in range(1, N):
		if arr[i] > arr[i - 1]:
			s += arr[i]
		else:
			m = max(s, m)
			s = arr[i]
	return max(m, s)


def largestSum(arr, N):
	sumN = 0
	A = []
	if N == 1:
		return arr[0]
	for i in range(N - 1):
		if arr[i] < arr[i + 1]:
			if i == N - 2:
				sumN = sumN + arr[i] + arr[i + 1]
				A.append(sumN)
			sumN = sumN + arr[i]
		else:
			sumN = sumN + arr[i]
			A.append(sumN)
			sumN = 0
	return max(A)
t = int(input())
for _ in range(0, t):
	N = int(input())
	arr = list(map(int, input().split()))
	print(largestSum(arr, N))


def largestSum(arr, N):
	s = 0
	mx = float('-inf')
	for i in range(0, len(arr) - 1):
		s = s + arr[i]
		if arr[i + 1] <= arr[i]:
			mx = max(mx, s)
			s = 0
	s = s + arr[len(arr) - 1]
	mx = max(mx, s)
	return mx


def largestSum(arr, N):
	maxx = -float('inf')
	summ = arr[0]
	for i in range(1, N):
		if arr[i - 1] < arr[i]:
			summ += arr[i]
		else:
			maxx = max(maxx, summ)
			summ = arr[i]
	return max(maxx, summ)


def largestSum(arr, n):
	r = -1000000
	for i in range(n):
		curr = arr[i]
		while i + 1 < n and arr[i] < arr[i + 1]:
			curr += arr[i + 1]
			i = i + 1
		if curr > r:
			r = curr
	return r


def largestSum(arr, N):
	curr_sum = max_sum = arr[0]
	for i in range(1, N):
		if arr[i] > arr[i - 1]:
			curr_sum += arr[i]
		else:
			curr_sum = arr[i]
		max_sum = max(max_sum, curr_sum)
	return max_sum


def largestSum(arr, N):
	ans = []
	s = 0
	for i in range(N - 1):
		s += arr[i]
		if arr[i] >= arr[i + 1]:
			ans.append(s)
			s = 0
	if arr[N - 1] > arr[N - 2]:
		s += arr[N - 1]
		ans.append(s)
	else:
		ans.append(s)
		ans.append(arr[N - 1])
	return max(ans)


def largestSum(arr, n):
	k = arr[0]
	mx = arr[0]
	for i in range(1, n):
		if arr[i] > arr[i - 1]:
			k += arr[i]
			mx = max(mx, k)
		else:
			k = arr[i]
	return mx


def largestSum(arr, N):
	currentAmount = arr[0]
	maximumMoney = currentAmount
	for i in range(1, N):
		if arr[i] > arr[i - 1]:
			currentAmount += arr[i]
		else:
			currentAmount = arr[i]
		if currentAmount > maximumMoney:
			maximumMoney = currentAmount
	return maximumMoney


def largestSum(arr, N):
	res = -2 ** 32
	for i in range(N):
		curr_sum = arr[i]
		while i + 1 < N and arr[i + 1] > arr[i]:
			curr_sum += arr[i + 1]
			i += 1
		if res < curr_sum:
			res = curr_sum
	return res


def largestSum(a, N):
	sum = a[0]
	maxsum = a[0]
	for i in range(1, N):
		if a[i] <= a[i - 1]:
			if sum > maxsum:
				maxsum = sum
			sum = 0
		sum += a[i]
		if i == N - 1:
			if sum > maxsum:
				maxsum = sum
	return maxsum


def largestSum(arr, N):
	a = 0
	b = 0
	c = []
	for i in range(N):
		if arr[i] > a:
			b += arr[i]
			a = arr[i]
		else:
			c.append(b)
			b = arr[i]
			a = arr[i]
		if i == N - 1:
			c.append(b)
	c.sort()
	if len(c) > 0:
		return c[-1]
	else:
		return 0


def largestSum(arr, N):
	temp_no = arr[0]
	score = 0
	score += temp_no
	win = score
	for x in arr[1:]:
		if temp_no < x:
			score += x
			temp_no = x
		else:
			temp_no = x
			score = x
		if score > win:
			win = score
	return win