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 than 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++
// 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);
} |
Java
// 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. |
Python3
# 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" |
C#
// 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. |
PHP
<?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. ?> |
Javascript
<script> // javascript Code 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
var sum = 0;
for (i = 0; i < n; i++)
// computes sum
sum += Math.log(a[i]);
// calculates the elements
// according to formula.
var x = parseInt( Math.exp(sum / n));
// returns the minimal element
return x + 1;
}
/* Driver program to test above function */
// initialised array
var a = [ 3, 2, 1, 4 ];
// computes the size of array
var n = a.length;
// prints out the minimal element
document.write(findMin(a, n));
// This code is contributed by aashish1995 </script> |
Output
3
Time Complexity: O(n * log(logn))
Auxiliary Space: O(1)
Recommended Articles