Given an array of n elements, update all elements of given array to some minimum value x i.e, arr[i] = x (0 <= i < n), such that product of all elements of this new array is strictly greater than the product of all elements of the initial array.
Example 1:
Input:
N=5
arr[] = {4, 2, 1, 10, 6} 
Output: 4
Explanation:
4 is the smallest value such that  
4 * 4 * 4 * 4 * 4 > 4 * 2 * 1 * 10 * 6.  
Example 2:
Input:
N=5
arr[] = {100, 150, 10000, 123458, 90980454} 
Output: 17592
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 findMinValue() that takes array arr and n as parameters and return the desired minimum value.
 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 10^{5}
 1 <= arr[i] <= 10^{10}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:
    def findMinValue(self,a,n):
        #code here.

Solutions:
import math

def binarysearch(n, l, r, k):
	if l <= r:
		mid = (l + r) // 2
		if math.log(mid) * n > k:
			return binarysearch(n, l, mid - 1, k)
		return binarysearch(n, mid + 1, r, k)
	return l

class Solution:

	def findMinValue(self, a, n):
		val = 0
		for i in range(n):
			val += math.log(a[i])
		return binarysearch(n, min(a), max(a), val)


import math
EPS = 1e-15

class Solution:

	def findMinValue(self, a, n):
		summ = 0
		for i in range(n):
			summ += math.log10(a[i]) + EPS
		x1 = summ / n + EPS
		res = math.pow(10.0, x1) + EPS
		return math.ceil(res + EPS)


import math

class Solution:

	def findMinValue(self, arr, n):
		EPS = 1e-15
		sum = 0
		for i in range(n):
			sum += math.log10(arr[i]) + EPS
		xl = sum / n + EPS
		res = math.pow(10.0, xl) + EPS
		return math.ceil(res + EPS)


import math

class Solution:

	def findMinValue(self, a, n):
		a.sort()
		val = 0
		for i in range(n):
			val += math.log(a[i])
		left = a[0]
		right = a[n - 1] + 1
		while left <= right:
			mid = (right + left) // 2
			temp = n * math.log(mid)
			if val < temp:
				ans = mid
				right = mid - 1
			else:
				left = mid + 1
		return ans