Given an array Arr of size N with all elements greater than or equal to zero. Return the maximum product of two numbers possible.
Example 1:
Input: 
N = 6
Arr[] = {1, 4, 3, 6, 7, 0}  
Output: 42
Example 2:
Input: 
N = 5
Arr = {1, 100, 42, 4, 23}
Output: 4200
Your Task:  
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr[] and n as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
2 ≤ N ≤ 10^{7}
0 ≤ Arr[i] ≤ 10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Data structures

Starter code: #User function Template for python3
class Solution:

	def maxProduct(self,arr, n):
		# code here

Solutions:
class Solution:

	def maxProduct(self, arr, n):
		arr.sort()
		return arr[n - 1] * arr[n - 2]


class Solution:

	def maxProduct(self, arr, n):
		max1 = max2 = float('-inf')
		for num in arr:
			if num > max1:
				max2 = max1
				max1 = num
			elif num > max2:
				max2 = num
		return max1 * max2


class Solution:

	def maxProduct(self, arr, n):
		s = sorted(arr)
		return s[n - 1] * s[n - 2]


class Solution:

	def maxProduct(self, arr, n):
		b = sorted(arr)
		a = b[-1] * b[-2]
		return a


class Solution:

	def maxProduct(self, arr, n):
		max1 = max(arr)
		arr.remove(max1)
		max2 = max(arr)
		return max1 * max2


class Solution:

	def maxProduct(self, arr, n):
		max1 = 0
		max2 = 0
		for num in arr:
			if num >= max1:
				max2 = max1
				max1 = num
			elif num > max2:
				max2 = num
		return max1 * max2


class Solution:

	def maxProduct(self, arr, n):
		a = max(arr[:])
		arr.remove(a)
		b = max(arr[:])
		return a * b


class Solution:

	def maxProduct(self, arr, n):
		arr.sort()
		return max(arr[-1] * arr[-2], arr[0] * arr[1])


class Solution:

	def maxProduct(self, arr, n):
		arr.sort(reverse=True)
		return arr[0] * arr[1]


class Solution:

	def maxProduct(self, arr, n):
		(max1, max2) = (0, 0.0)
		for i in range(n):
			if arr[i] > max1:
				max2 = max1
				max1 = arr[i]
			elif arr[i] > max2:
				max2 = arr[i]
		return max1 * max2


class Solution:

	def maxProduct(self, arr, n):
		arr.sort()
		a = arr.pop(n - 1)
		b = arr.pop(n - 2)
		return a * b


class Solution:

	def maxProduct(self, arr, n):
		arr.sort()
		num1 = num2 = 0
		sum1 = arr[0] * arr[1]
		sum2 = arr[n - 1] * arr[n - 2]
		if sum1 > sum2:
			num1 = arr[0]
			num2 = arr[1]
		else:
			num1 = arr[n - 2]
			num2 = arr[n - 1]
			return num1 * num2


class Solution:

	def maxProduct(self, arr, n):
		m1 = arr[0]
		m2 = -1
		for i in range(1, n):
			if arr[i] >= m1:
				m2 = m1
				m1 = arr[i]
			elif arr[i] > m2:
				m2 = arr[i]
		return m1 * m2


class Solution:

	def maxProduct(self, arr, n):
		(mx, mx2) = (0, 0)
		for i in range(n):
			if arr[i] > mx:
				mx2 = mx
				mx = arr[i]
			elif arr[i] > mx2:
				mx2 = arr[i]
		return mx * mx2


class Solution:

	def maxProduct(self, arr, n):
		arr.sort()
		y = arr[-1]
		z = arr[-2]
		x = y * z
		return x


class Solution:

	def maxProduct(self, arr, n):
		x = max(arr)
		for i in range(n):
			if arr[i] == x:
				arr[i] = -123345
				break
		y = max(arr)
		return x * y


class Solution:

	def maxProduct(self, arr, n):
		arr.sort()
		if len(arr) >= 2:
			prod = arr[-1] * arr[-2]
		return prod


class Solution:

	def maxProduct(self, arr, n):
		m1 = max(arr)
		arr.remove(m1)
		m2 = max(arr)
		return m1 * m2


class Solution:

	def maxProduct(self, arr, n):
		arr.sort(reverse=True)
		for i in range(n):
			max_prod = arr[0] * arr[1]
		return max_prod


class Solution:

	def maxProduct(self, arr, n):
		arr.sort(reverse=True)
		mul = 1
		for i in range(len(arr)):
			if arr[i] >= 0:
				mul = arr[0] * arr[1]
		return mul


class Solution:

	def maxProduct(self, arr, n):
		first = 0
		second = 0
		for i in range(n):
			if first < arr[i]:
				second = first
				first = arr[i]
			elif second < arr[i]:
				second = arr[i]
		return first * second


class Solution:

	def maxProduct(self, arr, n):
		max_1 = max(arr)
		arr.pop(arr.index(max_1))
		max_2 = max(arr)
		return max_1 * max_2


class Solution:

	def maxProduct(self, arr, n):
		(max_n, max_p) = (arr[0], arr[0])
		max_product = float('-inf')
		for idx in range(1, len(arr)):
			num = arr[idx]
			if num < 0:
				max_product = max(max_product, max_n * num)
				max_n = min(max_n, num)
			else:
				max_product = max(max_product, max_p * num)
				max_p = max(max_p, num)
		return max_product


class Solution:

	def maxProduct(self, arr, n):
		first_max = 0
		second_max = 0
		for i in range(n):
			if arr[i] >= first_max:
				second_max = first_max
				first_max = arr[i]
			elif arr[i] >= second_max:
				second_max = arr[i]
			else:
				continue
		return first_max * second_max


class Solution:

	def maxProduct(self, arr, n):
		arr.sort()
		maxi = 0
		for i in range(n - 1):
			product = arr[i] * arr[i + 1]
			maxi = max(maxi, product)
		return maxi


class Solution:

	def maxProduct(self, arr, n):
		arr1 = sorted(arr)
		max = int(arr1[n - 1] * arr1[n - 2])
		return max


class Solution:

	def maxProduct(self, arr, n):
		res = 1
		for i in range(2):
			res = res * max(arr)
			arr.remove(max(arr))
		return res


class Solution:

	def maxProduct(self, arr, n):
		arr.sort()
		res = []
		for i in range(n - 2, n):
			res.append(arr[i])
		return str(res[0] * res[1])


class Solution:

	def maxProduct(self, arr, n):
		k = max(arr)
		arr.remove(k)
		return k * max(arr)


class Solution:

	def maxProduct(self, arr, n):
		arr.sort()
		for i in range(len(arr)):
			s = arr[n - 1] * arr[n - 2]
		return s


class Solution:

	def maxProduct(self, arr, n):
		arr.sort()
		p = 1
		for i in range(n - 1, n - 3, -1):
			p = p * arr[i]
		return p


class Solution:

	def maxProduct(self, arr, n):
		fmaxi = max(arr)
		arr.remove(fmaxi)
		smaxi = max(arr)
		return fmaxi * smaxi


class Solution:

	def maxProduct(self, arr, n):
		l = []
		arr.sort()
		a = arr[-1] * arr[-2]
		return a


class Solution:

	def maxProduct(self, arr, n):
		x = max(arr)
		arr.remove(x)
		y = max(arr)
		return x * y


class Solution:

	def maxProduct(self, arr, n):
		m = max(arr)
		arr.remove(m)
		n = max(arr)
		return m * n


class Solution:

	def maxProduct(self, arr, n):
		(first, second) = (arr[0], arr[1]) if arr[0] >= arr[1] else (arr[1], arr[0])
		for index in range(2, n):
			if arr[index] > first:
				second = first
				first = arr[index]
			elif arr[index] > second:
				second = arr[index]
		return first * second


class Solution:

	def maxProduct(self, arr, n):
		first = -1
		second = -1
		for i in arr:
			if i > first:
				second = first
				first = i
			elif i > second:
				second = i
		return first * second