Open In App

Minimum element whose n-th power is greater than product of an array of size n

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.    
Python
# 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. 
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>
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.
?>

Output
3

Time Complexity: O(n * log(logn)) 
Auxiliary Space: O(1) 

Sorting approach:

Follow the below Steps:

  1. Go through the array one more time and get the result of all the elements of the array. This result will be used as the starting point for comparing the modified array.
  2. Sort the array in non decreasing order.
  3. Iterate from smallest to largest in the sorted array.
  4. Assign x to each element of the sorted array starting from the current one. Increase x until the sum of all changed array elements is greater than the sum of the original array elements. The smallest x that achieves this result is the solution.
  5. Return the minimum value of x found in step 4 as the output.

Below is the implementation of above approach:

Java
import java.util.Arrays;

public class Main {
    public static void main(String[] args)
    {
        int[] arr = { 3, 2, 1, 4 };
        System.out.println(findMinX(arr));
    }

    public static int findMinX(int[] arr)
    {
        // Calculate the product of all elements in the
        // array
        long product = 1;
        for (int num : arr) {
            product *= num;
        }

        // Sort the array in non-decreasing order
        Arrays.sort(arr);

        // Calculate the minimum x
        int min_x = 1;
        for (int num : arr) {
            if (Math.pow(min_x, arr.length) > product) {
                break;
            }
            min_x = num;
        }

        return min_x;
    }
}
Python
def find_min_x(arr):
    # Calculate the product of all elements in the array
    product = 1
    for num in arr:
        product *= num

    # Sort the array in non-decreasing order
    arr.sort()

    # Calculate the minimum x
    min_x = 1
    for num in arr:
        if min_x ** len(arr) > product:
            break
        min_x = num

    return min_x


# Example usage:
arr = [3, 2, 1, 4]

print(find_min_x(arr))
JavaScript
function findMinX(arr) {
    // Calculate the product of all elements in the array
    let product = 1;
    for (let num of arr) {
        product *= num;
    }

    // Sort the array in non-decreasing order
    arr.sort((a, b) => a - b);

    // Calculate the minimum x
    let min_x = 1;
    for (let num of arr) {
        // Check if min_x raised to the power of array length exceeds the product
        if (Math.pow(min_x, arr.length) > product) {
            break;
        }
        min_x = num;
    }

    return min_x;
}

// Example usage
let arr = [3, 2, 1, 4];
console.log(findMinX(arr));

Output
3

Time Complexity: O(n logn)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads