📜  查找最小值以分配所有数组元素,以使数组乘积变大

📅  最后修改于: 2021-04-26 10:13:23             🧑  作者: Mango

给定n个元素的数组arr [],将给定数组的所有元素更新为某个最小值x,即arr [i] = x(0 <= i

例子:

Input  : arr[] = [4, 2, 1, 10, 6]
Output :  4
4 is the smallest value such that 
4 * 4 * 4 * 4 * 4 > 4 * 2 * 1 * 10 * 6

Input  : arr[] = [100, 150, 10000, 123458, 90980454]
Output : 17592

方法1:O(n log n)
我们在n的极限上使用二进制搜索。在每个中间,我们检查n的乘积是否大于数组的原始乘积。
我们使用产品日志来避免溢出。因此,我们在二进制搜索期间计算当前乘积的对数和中n的对数以比较值。

C++
// C++ program to find minimum value that can
// be assigned to all elements so that product
// becomes greater than current product.
#include
#define ll long long
#define ld long double
using namespace std;
  
ll findMinValue(ll arr[], ll n)
{
    // sort the array to apply Binary search
    sort(arr, arr+n);
  
    // using log property add every logarithmic
    // value of element to val
    ld val = 0; // where ld is long double
    for (int i=0; i


Java
import java.util.Arrays;
  
// Java program to find minimum value that can 
// be assigned to along elements so that product 
// becomes greater than current product.
class GFG1 {
  
    static long findMinValue(long arr[], int n) {
        // sort the array to apply Binary search 
        Arrays.sort(arr);
  
        // using log property add every logarithmic 
        // value of element to val 
        double val = 0; // where double is long double 
        for (int i = 0; i < n; i++) {
            val += (double) (Math.log((double) (arr[i])));
        }
  
        // set left and right extremities to find 
        // min value 
        long left = arr[0], right = arr[n - 1];
  
        long ans = 0;
        while (left <= right) {
            long mid = (left + right) / 2;
  
            // multiplying n to mid, to find the 
            // correct min value 
            double temp = (double) n * (double) (Math.log((double) (mid)));
            if (val < temp) {
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }
  
// Driver code 
    public static void main(String[] args) {
  
        long arr[] = {4, 2, 1, 10, 6};
        int n = arr.length;
        System.out.println(findMinValue(arr, n));
  
    }
}
//This code is contributed by 29AjayKumar


Python3
# Python3 program to find minimum 
# value that can be assigned to all 
# elements so that product becomes 
# greater than current product.
import math
  
def findMinValue(arr, n):
  
    # sort the array to apply
    # Binary search
    arr.sort()
  
    # using log property add every 
    # logarithmic value of element to val
    val = 0 # where ld is long double
    for i in range(n):
        val += (math.log(arr[i]))
  
    # set left and right extremities to find
    # min value
    left = arr[0]
    right = arr[n - 1] + 1
  
    while (left <= right):
        mid = (left + right) // 2
  
        # multiplying n to mid, to find 
        # the correct min value
        temp = n * (math.log(mid))
        if (val < temp):
            ans = mid
            right = mid - 1
        else:
            left = mid + 1
    return ans
  
# Driver code
if __name__ == "__main__":
    arr = [4, 2, 1, 10, 6]
    n = len(arr)
    print(findMinValue(arr, n) )
  
# This code is contributed 
# by ChitraNayal


C#
// C#  program to find minimum value that can 
// be assigned to along elements so that product 
// becomes greater than current product.
  
using System;
  
public class GFG{
      
    static long findMinValue(long []arr, int n) {
        // sort the array to apply Binary search 
        Array.Sort(arr);
  
        // using log property add every logarithmic 
        // value of element to val 
        double val = 0; // where double is long double 
        for (int i = 0; i < n; i++) {
            val += (double) (Math.Log((double) (arr[i])));
        }
  
        // set left and right extremities to find 
        // min value 
        long left = arr[0], right = arr[n - 1];
  
        long ans = 0;
        while (left <= right) {
            long mid = (left + right) / 2;
  
            // multiplying n to mid, to find the 
            // correct min value 
            double temp = (double) n * (double) (Math.Log((double) (mid)));
            if (val < temp) {
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }
  
// Driver code 
    static public void Main (){
        long []arr = {4, 2, 1, 10, 6};
        int n = arr.Length;
        Console.WriteLine(findMinValue(arr, n));
    }
//This code is contributed by ajit.
}


PHP


C++
// C++ program to find minimum value to assign all 
// array elements so that array product becomes greater
#include 
using namespace std;
  
// Epsilon value is used at various steps
// to ensure correctness upto 10^15 digits.
#define EPS 1e-15
#define ll long long int
  
ll findMinValue(ll arr[], ll n)
{
    // add logarithmic value of all elements to sum
    long double sum = 0;
    for (int i=0; i


Java
// Java program to find minimum value to assign all array 
// elements so that array product becomes greater
class GFG{
  
// Epsilon value is used at various steps 
// to ensure correctness upto 10^15 digits. 
static double EPS=1E-15; 
  
static double findMinValue(double arr[], double n) 
{ 
    // add logarithmic value of all elements to sum 
    double sum = 0; 
    for (int i=0; i


Python3
# Epsilon value is used at various steps 
# to ensure correctness upto 10^15 digits.
import math
EPS = 1E-15; 
  
def findMinValue(arr, n): 
  
    # add logarithmic value of all 
    # elements to sum 
    sum = 0; 
    for i in range(n): 
        sum += math.log10(arr[i]) + EPS; 
  
    # to find the nth root of sum 
    xl = (sum / n + EPS); 
  
    # to find the antilog of xl 
    res = math.pow(10.0, xl) + EPS; 
    return math.ceil(res + EPS); 
  
# Driver code 
arr = [4, 2, 1, 10, 6]; 
n = len(arr); 
print(findMinValue(arr, n)); 
  
# This code is contributed by mits


C#
// C# program to find minimum value to assign all
// array elements so that array product becomes greater
using System;
class GFG{
  
// Epsilon value is used at various steps 
// to ensure correctness upto 10^15 digits. 
static double EPS=1E-15; 
  
static double findMinValue(double[] arr, double n) 
{ 
    // add logarithmic value of all elements to sum 
    double sum = 0; 
    for (int i=0; i


PHP


输出 :

4

解2:O(n)
通过知道n个元素的乘积为P的事实,如果我们必须找到P的第n个根。要找到乘积的第n个根,我们可以简单地将n从数组n个元素的对数的和中除掉,然后antilog的面纱将是我们对问题的答案,即
ans = ceil(antilog(log(x)/ n))
ans = ceil(功率(10,log(x)/ n))

C++

// C++ program to find minimum value to assign all 
// array elements so that array product becomes greater
#include 
using namespace std;
  
// Epsilon value is used at various steps
// to ensure correctness upto 10^15 digits.
#define EPS 1e-15
#define ll long long int
  
ll findMinValue(ll arr[], ll n)
{
    // add logarithmic value of all elements to sum
    long double sum = 0;
    for (int i=0; i

Java

// Java program to find minimum value to assign all array 
// elements so that array product becomes greater
class GFG{
  
// Epsilon value is used at various steps 
// to ensure correctness upto 10^15 digits. 
static double EPS=1E-15; 
  
static double findMinValue(double arr[], double n) 
{ 
    // add logarithmic value of all elements to sum 
    double sum = 0; 
    for (int i=0; i

Python3

# Epsilon value is used at various steps 
# to ensure correctness upto 10^15 digits.
import math
EPS = 1E-15; 
  
def findMinValue(arr, n): 
  
    # add logarithmic value of all 
    # elements to sum 
    sum = 0; 
    for i in range(n): 
        sum += math.log10(arr[i]) + EPS; 
  
    # to find the nth root of sum 
    xl = (sum / n + EPS); 
  
    # to find the antilog of xl 
    res = math.pow(10.0, xl) + EPS; 
    return math.ceil(res + EPS); 
  
# Driver code 
arr = [4, 2, 1, 10, 6]; 
n = len(arr); 
print(findMinValue(arr, n)); 
  
# This code is contributed by mits

C#

// C# program to find minimum value to assign all
// array elements so that array product becomes greater
using System;
class GFG{
  
// Epsilon value is used at various steps 
// to ensure correctness upto 10^15 digits. 
static double EPS=1E-15; 
  
static double findMinValue(double[] arr, double n) 
{ 
    // add logarithmic value of all elements to sum 
    double sum = 0; 
    for (int i=0; i

的PHP


输出 :

4