Minimum element whose n-th power is greater than product of an array of size n
Given an array of n integers. Find minimum x which is to be assigned to every array element such that product of all elements of this new array is strictly greater than product of all elements of the initial array.
Input: 4 2 1 10 6 Output: 4 Explanation: Product of elements of initial array 4*2*1*10*6 = 480. If x = 4 then 4*4* 4*4*4 = 480, if x = 3 then 3*3*3*3*3=243. So minimal element = 4 Input: 3 2 1 4 Output: 3 Explanation: Product of elements of initial array 3*2*1*4 = 24. If x = 3 then 3*3*3*3 = 81, if x = 2 then 2*2*2*2 = 243. So minimal element = 3.
Simple Approach: A simple approach is to run a loop from 1 till we find the product is greater than the initial array product.
Time Complexity : O(x^n) and if used pow function then O(x * log n)
Let, x^n = a1 * a2 * a3 * a4 *....* an we have been given n and value of a1, a2, a3, ..., an. Now take log on both sides with base e n*logex > loge(a1) + loge(a2) +......+ loge(an) Lets sum = loge(a1) + loge(a2) + ...... + loge(an) n*loge x > sum loge x > sum/n Then take antilog on both side x > e^(sum/n)
Below is the implementation of above approach.
Time Complexity: O(n * log(logn))
Auxiliary Space: O(1)
This article is contributed by Raja Vikramaditya. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.