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 then 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 contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find minimum value to assign all array elements so that array product becomes greater
- Sum and Product of minimum and maximum element of an Array
- Product of all Subsequences of size K except the minimum and maximum Elements
- Replace each element by the difference of the total size of the array and frequency of that element
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Number of pairs in an array such that product is greater than sum
- Closest greater element for every array element from another array
- Maximum product quadruple (sub-sequence of size 4) in array
- Maximum product of a triplet (subsequnece of size 3) in array
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Smallest element greater than X not present in the array
- Elements greater than the previous and next element in an Array
- Find closest greater value for every element in array
- Minimum 1s to lend power to make whole array powerful
- Adding elements of an array until every element becomes greater than or equal to k