Chunky gets happy by eating Melody.
Given an array of N elements, each element represents happiness chunky get by eating melody.
Chunky knows why melody is so chocolaty but will only tell you once you tell him the Max happiness he can get by eating two adjacent melodies.
Example 1:
Ã¢â¬â¹Input : arr[ ] = {1, 2, 3, 4, 5}
Output : 9
Explanation:
At index 0, arr[0] + arr[1] = 3
At index 1, arr[1] + arr[2] = 5
At index 2, arr[2] + arr[3] = 7
...
In this way, 9 will be the answer.
Ã¢â¬â¹Example 2:
Input : arr[ ] = {1, 2, 3, 4} 
Output :  7
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function max_adjacent_sum() that takes an array (arr), sizeOfArray (n), and return the maximum happiness. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
2 ≤ N ≤ 10^{5}
-10^{7} ≤ A[i] ≤ 10^{7}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



def max_adjacent_sum (arr, n) : 

    #Complete the function



Solutions:
def max_adjacent_sum(arr, n):
	m = -99999999
	for i in range(1, n):
		m = max(m, arr[i] + arr[i - 1])
	return m


def max_adjacent_sum(arr, n):
	sumarr = []
	for i in range(1, n):
		sumarr.append(arr[i - 1] + arr[i])
	return max(sumarr)


def max_adjacent_sum(a, n):
	b = []
	for i in range(n - 1):
		b.append(arr[i] + arr[i + 1])
	return max(b)


def max_adjacent_sum(arr, n):
	mi = -9999999
	for i in range(n - 1):
		mi = max(mi, arr[i] + arr[i + 1])
	return mi


def max_adjacent_sum(arr, n):
	s = -99999
	s1 = 0
	for i in range(n - 1):
		s1 = arr[i] + arr[i + 1]
		s = max(s, s1)
	return s


def max_adjacent_sum(arr, n):
	max_sm = float('-inf')
	for index in range(n - 1):
		sm = arr[index] + arr[index + 1]
		max_sm = sm if sm > max_sm else max_sm
	return max_sm


def max_adjacent_sum(arr, n):
	ma = arr[0] + arr[1]
	for i in range(2, n):
		ma = max(ma, arr[i] + arr[i - 1])
	return ma


def max_adjacent_sum(arr, n):
	summ = -999999999
	for i in range(n - 1):
		summ = max(summ, arr[i] + arr[i + 1])
	return summ


def max_adjacent_sum(arr, n):
	max_adj_sum = -1000000007
	for i in range(0, n - 1, 1):
		max_adj_sum = max(max_adj_sum, arr[i] + arr[i + 1])
	return max_adj_sum


def max_adjacent_sum(arr, n):
	max_happiness = -10 ** 5
	for i in range(1, n):
		if arr[i] + arr[i - 1] > max_happiness:
			max_happiness = arr[i] + arr[i - 1]
	return max_happiness


def max_adjacent_sum(arr, n):
	max = -100000000
	for i in range(1, n):
		if arr[i] + arr[i - 1] > max:
			max = arr[i] + arr[i - 1]
	return max


def max_adjacent_sum(arr, n):
	k = []
	for i in range(n - 1):
		s = int(arr[i] + arr[i + 1])
		k.append(s)
	return max(k)


def max_adjacent_sum(arr, n):
	m = -10 ** 7 - 1
	i = 1
	while i < n:
		if arr[i] + arr[i - 1] > m:
			m = arr[i] + arr[i - 1]
		i += 1
	return m


def max_adjacent_sum(arr, n):
	maxi = -999999
	for i in range(len(arr) - 1):
		sum = arr[i] + arr[i + 1]
		if sum > maxi:
			maxi = sum
	return maxi


def max_adjacent_sum(arr, n):
	val = -99999999999
	for i in range(n - 1):
		ans = arr[i] + arr[i + 1]
		val = max(val, ans)
	return val


def max_adjacent_sum(arr, n):
	m = float('-inf')
	for i in range(n - 1):
		if arr[i] + arr[i + 1] > m:
			m = arr[i] + arr[i + 1]
	return m


def max_adjacent_sum(arr, n):
	summa = sum(arr[:2])
	maxi = summa
	for i in range(n - 2):
		summa += arr[i + 2] - arr[i]
		maxi = max(maxi, summa)
	return maxi


def max_adjacent_sum(arr, n):
	max1 = arr[0] + arr[1]
	for i in range(1, n - 1):
		max2 = arr[i] + arr[i + 1]
		if max2 > max1:
			max1 = max2
	return max1


def max_adjacent_sum(arr, n):
	tot = float('-inf')
	for i in range(n - 1):
		tot = max(tot, arr[i] + arr[i + 1])
	return tot


def max_adjacent_sum(arr, n):
	max = arr[0] + arr[1]
	sum = 0
	for i in range(1, n - 1):
		sum = arr[i] + arr[i + 1]
		if sum > max:
			max = sum
	return max


def max_adjacent_sum(arr, n):
	k = -1000000000
	for i in range(n - 1):
		j = arr[i] + arr[i + 1]
		if j > k:
			k = j
	return k


def max_adjacent_sum(arr, n):
	max_sum = -99999999999
	for i in range(0, len(arr) - 1):
		if arr[i] + arr[i + 1] > max_sum:
			max_sum = arr[i] + arr[i + 1]
	return max_sum


def max_adjacent_sum(arr, n):
	m = -10 ** 8
	for i in range(n - 1):
		x = arr[i] + arr[i + 1]
		if x > m:
			m = x
	return m


def max_adjacent_sum(arr, n):
	t = -11111111111111
	for i in range(0, n - 1):
		t = max(t, arr[i] + arr[i + 1])
	return t


def max_adjacent_sum(arr, n):
	res = -float('inf')
	for i in range(1, n):
		sum1 = arr[i - 1] + arr[i]
		res = max(sum1, res)
		if i + 1 < n:
			sum1 += arr[i + 1] - arr[i - 1]
		else:
			break
	return res


def max_adjacent_sum(arr, n):
	maxi = -10 ** 7
	sum = 0
	for i in range(n - 1):
		sum += arr[i] + arr[i + 1]
		maxi = max(maxi, sum)
		sum = 0
	return maxi


def max_adjacent_sum(arr, n):
	ans = arr[0] + arr[1]
	for i in range(n - 1):
		ans = max(ans, arr[i] + arr[i + 1])
	return ans


def max_adjacent_sum(arr, n):
	count = 0
	a = []
	for i in range(n):
		count = arr[i - 1] + arr[i]
		a.append(count)
	a.sort()
	return a[len(a) - 1]


def max_adjacent_sum(arr, n):
	su = -11111111111111111111111111111111111111111111
	for i in range(n - 1):
		su = max(su, arr[i] + arr[i + 1])
	return su


def max_adjacent_sum(arr, n):
	maxh = arr[0] + arr[1]
	for i in range(n - 1):
		if arr[i] + arr[i + 1] > maxh:
			maxh = arr[i] + arr[i + 1]
	return maxh


def max_adjacent_sum(arr, n):
	m = -10 ** 7
	for i in range(1, n):
		m = max(m, arr[i] + arr[i - 1])
	return m


def max_adjacent_sum(arr, n):
	mx = -10000000000007
	for x in range(1, n):
		mx = max(mx, arr[x] + arr[x - 1])
	return mx


def max_adjacent_sum(arr, n):
	max_sum = -99999
	for i in range(n - 1):
		max_sum = max(max_sum, arr[i] + arr[i + 1])
	return max_sum


def max_adjacent_sum(arr, n):
	res = arr[0]
	max_sum = -13456789009
	for i in range(n - 1):
		max_sum = max(max_sum, arr[i] + arr[i + 1])
	return max_sum


def max_adjacent_sum(arr, n):
	b = []
	for i in range(n - 1):
		b.append(arr[i] + arr[i + 1])
	return max(b)
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ans = max_adjacent_sum(arr, n)
	print(ans)


def max_adjacent_sum(arr, n):
	d = -99999999999
	for i in range(n - 1):
		e = arr[i] + arr[i + 1]
		if e > d:
			d = e
	return d
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ans = max_adjacent_sum(arr, n)
	print(ans)


def max_adjacent_sum(arr, n):
	maxSum = -9999999999
	for itr in range(n - 1):
		if maxSum < arr[itr] + arr[itr + 1]:
			maxSum = arr[itr] + arr[itr + 1]
	return maxSum
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ans = max_adjacent_sum(arr, n)
	print(ans)


def max_adjacent_sum(arr, n):
	melody = arr[0] + arr[1]
	for i in range(1, n - 1):
		x = arr[i] + arr[i + 1]
		if x > melody:
			melody = x
		else:
			pass
	return melody


def max_adjacent_sum(a, n):
	s = 0
	m = -1000000007
	for i in range(n - 1):
		s = arr[i] + arr[i + 1]
		if m < s:
			m = s
	return m


def max_adjacent_sum(arr, n):
	ansmax = -999999
	for i in range(n - 1):
		ansmax = max(ansmax, arr[i] + arr[i + 1])
	return ansmax


def max_adjacent_sum(arr, n):
	maxx = float('-inf')
	for i in range(0, n - 1):
		maxx = max(maxx, arr[i] + arr[i + 1])
	return maxx


def max_adjacent_sum(arr, n):
	maxe = -10 ** 9 - 7
	for i in range(n - 1):
		maxe = max(maxe, arr[i] + arr[i + 1])
	return maxe


def max_adjacent_sum(arr, n):
	maxi = float('-inf')
	for i in range(n - 1):
		a = arr[i] + arr[i + 1]
		if a > maxi:
			maxi = a
	return maxi


def max_adjacent_sum(arr, n):
	new_arr = arr.copy()
	new_arr.sort()
	f_sum = new_arr[0]
	for i in range(n - 1):
		adj_sum = arr[i] + arr[i + 1]
		if f_sum < adj_sum:
			f_sum = adj_sum
	return f_sum


def max_adjacent_sum(arr, n):
	maximum = -999999999999999
	for i in range(n - 1):
		ad_sum = arr[i] + arr[i + 1]
		if ad_sum > maximum:
			maximum = ad_sum
	return maximum