Given an array A[ ] your task is to complete the function max_val which finds the maximum value of abs(i – j) * min(arr[i], arr[j]) where i and j vary from 0 to n-1. 
Example 1:
Input:
N = 4
arr[] = 3 2 1 4
Output: 9
Explanation:
Here, a[0] = 3 and a[3] = 4 
and thus result is  
abs(0-3)*min(3,4) = 9.
Example 2:
Input:
N= 4
arr[] = 8 1 9 4
Output: 16
Explanation:
Here, a[0]=8 and a[2]=9 thus 
result is abs(0-2)*min(8,9)=16. 
Your task:
You don't need to read input or print anything. Your task is to complete the function max_val() which takes the array and it's size as input and returns the maximum value of abs(i – j) * min(arr[i], arr[j]).
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1<=N<=10^4
-10^5<=A[]<=10^5

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: '''The function returns an integer
which denotes the max value 
of abs(i â€“ j) * min(arr[i], arr[j]) 
in the array.'''
# You are required to complete this method
# You may use module's
class Solution:
    def max_val(self, arr, n):
        # code here

Solutions:
class Solution:

	def max_val(self, arr, n):
		lst = []
		i = 0
		j = n - 1
		while i < j:
			p = abs(i - j) * min(arr[i], arr[j])
			lst.append(p)
			if arr[i] < arr[j]:
				i += 1
			else:
				j -= 1
		return max(lst)


class Solution:

	def max_val(self, arr, n):
		ans = []
		i = 0
		j = n - 1
		while i < j:
			res = abs(i - j) * min(arr[i], arr[j])
			ans.append(res)
			if arr[i] < arr[j]:
				i += 1
			else:
				j -= 1
		return max(ans)


class Solution:

	def max_val(self, arr, n):
		max_val = -2131249124123123
		val = 0
		left = 0
		right = n - 1
		while left < right:
			if arr[left] < arr[right]:
				val = arr[left] * (right - left)
				left += 1
			else:
				val = arr[right] * (right - left)
				right -= 1
			max_val = max(max_val, val)
		return max_val


class Solution:

	def max_val(self, arr, n):
		(temp, first, sec) = ([], 0, n - 1)
		while first < sec:
			num = abs(first - sec) * min(arr[first], arr[sec])
			temp.append(num)
			if arr[first] < arr[sec]:
				first += 1
			else:
				sec -= 1
		return max(temp)


class Solution:

	def max_val(self, arr, n):
		mx = -1000000
		(i, j) = (0, n - 1)
		while i < j:
			if arr[i] < arr[j]:
				val = arr[i] * (j - i)
				i += 1
			else:
				val = arr[j] * (j - i)
				j -= 1
			mx = max(mx, val)
		return mx


class Solution:

	def max_val(self, arr, n):
		start = 0
		end = n - 1
		maxi = -1
		while start < end:
			absi = abs(start - end)
			mini = min(arr[start], arr[end])
			maxi = max(maxi, absi * mini)
			if arr[start] < arr[end]:
				start += 1
			else:
				end = end - 1
		return maxi


class Solution:

	def max_val(self, arr, n):
		(i, j) = (0, n - 1)
		m = 0
		while i < j:
			pro = (j - i) * min(arr[i], arr[j])
			if pro > m:
				m = pro
			if arr[i] < arr[j]:
				i += 1
			else:
				j -= 1
		return m


class Solution:

	def max_val(self, arr, n):
		l = []
		i = 0
		j = n - 1
		while i < j:
			r = abs(i - j) * min(arr[i], arr[j])
			l.append(r)
			if arr[i] < arr[j]:
				i += 1
			else:
				j -= 1
		return max(l)


class Solution:

	def max_val(self, arr, n):
		maxProduct = -2147483648
		currProduct = 0
		Left = 0
		right = n - 1
		while Left < right:
			if arr[Left] < arr[right]:
				currProduct = arr[Left] * (right - Left)
				Left += 1
			else:
				currProduct = arr[right] * (right - Left)
				right -= 1
			maxProduct = max(maxProduct, currProduct)
		return maxProduct


class Solution:

	def max_val(self, arr, n):
		max_val = -1234567890
		left = 0
		right = n - 1
		while left < right:
			res = abs(left - right) * min(arr[left], arr[right])
			max_val = max(max_val, res)
			if arr[left] > arr[right]:
				right -= 1
			else:
				left += 1
		return max_val


class Solution:

	def max_val(self, arr, n):
		maxV = float('-inf')
		i = 0
		j = n - 1
		while i < j:
			maxV = max(maxV, abs(i - j) * min(arr[i], arr[j]))
			if arr[i] < arr[j]:
				i += 1
			else:
				j -= 1
		return maxV
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split()))
		print(Solution().max_val(arr, n))


class Solution:

	def max_val(self, arr, n):
		ans = []
		i = 0
		j = n - 1
		while i < j:
			res = abs(i - j) * min(arr[i], arr[j])
			ans.append(res)
			if arr[i] < arr[j]:
				i += 1
			else:
				j -= 1
		return max(ans)
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split()))
		print(Solution().max_val(arr, n))


class Solution:

	def max_val(self, arr, n):
		li = []
		i = 0
		j = n - 1
		while i < j:
			li.append((j - i) * min(arr[i], arr[j]))
			if arr[i] < arr[j]:
				i += 1
			else:
				j -= 1
		li.sort()
		return li[-1]
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split()))
		print(Solution().max_val(arr, n))


class Solution:

	def max_val(self, arr, n):
		maxi = 10 ** (-10)
		i = 0
		j = n - 1
		while i < j:
			if maxi < abs(i - j) * min(arr[i], arr[j]):
				maxi = abs(i - j) * min(arr[i], arr[j])
			if arr[i] < arr[j]:
				i += 1
			else:
				j -= 1
		return maxi
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split()))
		print(Solution().max_val(arr, n))


class Solution:

	def max_val(self, arr, n):
		low = 0
		high = n - 1
		Max = -100000000000.0
		while low < high:
			res = abs(low - high) * min(arr[low], arr[high])
			Max = max(Max, res)
			if arr[low] < arr[high]:
				low += 1
			else:
				high -= 1
		return Max


class Solution:

	def max_val(self, arr, n):
		j = n - 1
		s = 0
		i = 0
		while i < j:
			s = max(abs(j - i) * min(arr[j], arr[i]), s)
			if arr[i] < arr[j]:
				i += 1
			else:
				j -= 1
		return s


class Solution:

	def max_val(self, arr, n):
		maxi = float('-inf')
		i = 0
		j = n - 1
		while i < j:
			maxi = max(maxi, abs(i - j) * min(arr[i], arr[j]))
			if arr[i] > arr[j]:
				j -= 1
			else:
				i += 1
		return maxi


class Solution:

	def max_val(self, arr, n):
		m = -100001
		first = 0
		lst = n - 1
		while first < lst:
			if arr[first] < arr[lst]:
				cur_m = (lst - first) * arr[first]
				first += 1
			else:
				cur_m = (lst - first) * arr[lst]
				lst -= 1
			m = max(m, cur_m)
		return m


class Solution:

	def max_val(self, arr, n):
		i = 0
		j = n - 1
		t = 0
		while i < j:
			if t < abs(i - j) * min(arr[i], arr[j]):
				t = abs(i - j) * min(arr[i], arr[j])
			if arr[i] < arr[j]:
				i += 1
			elif arr[j] < arr[i]:
				j -= 1
			else:
				i += 1
				j -= 1
		return t