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 firstname.lastname@example.org. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Find just strictly greater element from first array for each element in second array
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count pairs in Array whose product is a Kth power of any positive integer
- Number of pairs in an array such that product is greater than sum
- Replace each element by the difference of the total size of the array and frequency of that element
- Closest greater element for every array element from another array
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- Count pairs in array such that one element is power of another
- Maximum product quadruple (sub-sequence of size 4) in array
- Maximum product of a triplet (subsequence 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
- Count of greater elements for each element in the Array
- Elements greater than the previous and next element in an Array
- Find the next greater element in a Circular Array