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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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 pairs in Array whose product is a Kth power of any positive integer
- 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
- 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 pairs in array such that one element is power of another
- Count of elements in first Array greater than second Array with each element considered only once
- Min difference between maximum and minimum element in all Y size subarrays
- Maximum product of a triplet (subsequence of size 3) in array
- Maximum product quadruple (sub-sequence of size 4) in array
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Minimum cost required to convert all Subarrays of size K to a single element
- Minimum 1s to lend power to make whole array powerful
- Smallest subarray whose product leaves remainder K when divided by size of the array