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.

Examples:

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)

Mathematical Approach:

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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find minimum element whose n-th
// power is greater than product of an array of 
// size n
#include <bits/stdc++.h>
using namespace std;
  
// function to find the minimum element
int findMin(int a[], int n)
{
    // loop to traverse and store the sum of log
    double sum = 0;
    for (int i = 0; i < n; i++) 
        sum += log(a[i]); // computes sum
  
    // calculates the elements according to formula.
    int x = exp(sum / n);
  
    // returns the minimal element
    return x + 1;
}
  
// Driver program to test above function
int main()
{
    // initialised array
    int a[] = { 3, 2, 1, 4 };
  
    // computes the size of array
    int n = sizeof(a) / sizeof(a[0]);
  
    // prints out the minimal element
    cout << findMin(a, n);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA Code to find Minimum element whose
// n-th power is greater than product of
// an array of size n
import java.util.*;
  
class GFG {
      
    // function to find the minimum element
    static int findMin(int a[], int n)
    {
        // loop to traverse and store the 
        // sum of log
        double sum = 0;
        for (int i = 0; i < n; i++)
              
            // computes sum
            sum += Math.log(a[i]); 
       
        // calculates the elements 
        // according to formula.
        int x = (int)Math.exp(sum / n);
       
        // returns the minimal element
        return x + 1;
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
        // initialised array
        int a[] = { 3, 2, 1, 4 };
       
        // computes the size of array
        int n = a.length;
       
        // prints out the minimal element
        System.out.println(findMin(a, n));
          
    }
}
  
// This code is contributed by Arnav Kr. Mandal.    

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find minimum element
# whose n-th power is greater than product
# of an array of size n
import math as m
  
# function to find the minimum element
def findMin( a, n):
      
    # loop to traverse and store the
    # sum of log
    _sum = 0
    for i in range(n):
        _sum += m.log(a[i]) # computes sum
      
    # calculates the elements 
    # according to formula.
    x = m.exp(_sum / n)
      
    # returns the minimal element
    return int(x + 1)
      
# Driver program to test above function
  
# initialised array
a = [ 3, 2, 1, 4 ]
  
# computes the size of array
n = len(a)
  
# prints out the minimal element
print(findMin(a, n))
  
# This code is contributed by "Abhishek Sharma 44"

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Code to find Minimum element whose
// n-th power is greater than product of
// an array of size n
using System;
  
class GFG {
      
    // function to find the minimum element
    static int findMin(int []a, int n)
    {
          
        // loop to traverse and store the 
        // sum of log
        double sum = 0;
        for (int i = 0; i < n; i++)
              
            // computes sum
            sum += Math.Log(a[i]); 
      
        // calculates the elements 
        // according to formula.
        int x = (int)Math.Exp(sum / n);
      
        // returns the minimal element
        return x + 1;
    }
      
    /* Driver program to test above function */
    public static void Main()
    {
          
        // initialised array
        int []a = { 3, 2, 1, 4 };
      
        // computes the size of array
        int n = a.Length;
      
        // prints out the minimal element
        Console.WriteLine(findMin(a, n));
          
    }
}
  
// This code is contributed by vt_m. 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find minimum
// element whose n-th power
// is greater than product of 
// an array of size n
  
// function to find the
// minimum element
function findMin($a, $n)
{
      
    // loop to traverse and 
    // store the sum of log
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
      
        // computes sum
        $sum += log($a[$i]); 
  
    // calculates the elements
    // according to formula.
    $x = exp($sum / $n);
  
    // returns the minimal element
    return (int)($x + 1);
}
  
// Driver Code
$a = array( 3, 2, 1, 4 );
  
// computes the size of array
$n = sizeof($a);
  
// prints out the minimal element
echo(findMin($a, $n));
  
// This code is contributed by Ajit.
?>

chevron_right



Output:

3

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 contribute@geeksforgeeks.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.



My Personal Notes arrow_drop_up

Improved By : vt_m, jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.