Minimum element whose n-th power is greater than product of an array of size n
Last Updated :
03 May, 2024
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.
?>
Time Complexity: O(n * log(logn))
Auxiliary Space: O(1)
Sorting approach:
Follow the below Steps:
- 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.
- Sort the array in non decreasing order.
- Iterate from smallest to largest in the sorted array.
- 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.
- Return the minimum value of x found in step 4 as the output.
Below is the implementation of above approach:
C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int findMinX(vector<int>& arr)
{
// Calculate the product of all elements in the array
long long product = 1;
for (int num : arr) {
product *= num;
}
// Sort the array in non-decreasing order
sort(arr.begin(), arr.end());
// Set min_x to 3
int min_x = 3;
// Calculate the minimum x
for (int num : arr) {
if ((long long)min_x * arr.size() > product) {
break;
}
min_x = num;
}
return min_x;
}
int main()
{
vector<int> arr = { 3, 2, 1, 4 };
cout << findMinX(arr) << endl;
return 0;
}
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));
Time Complexity: O(n logn)
Auxiliary Space: O(1)
Similar Reads
Minimum steps to make sum and the product of all elements of array non-zero Given an array arr of N integers, the task is to find the minimum steps in which the sum and product of all elements of the array can be made non-zero. In one step any element of the array can be incremented by 1.Examples: Input: N = 4, arr[] = {0, 1, 2, 3} Output: 1 Explanation: As product of all e
7 min read
Minimum sum of product of elements of pairs of the given array Given an array arr[] of even number of element N in it. The task is to form N/2 pairs such that sum of product of elements in those pairs is minimum. Examples Input: arr[] = { 1, 6, 3, 1, 7, 8 } Output: 270 Explanation: The pair formed are {1, 1}, {3, 6}, {7, 8} Product of sum of these pairs = 2 * 9
5 min read
Maximum size of subset such that product of all subset elements is a factor of N Given an integer N and an array arr[] having M integers, the task is to find the maximum size of the subset such that the product of all elements of the subset is a factor of N. Examples: Input: N = 12, arr[] = {2, 3, 4}Output: 2Explanation: The given array 5 subsets such that the product of all ele
6 min read
Smallest power of 2 which is greater than or equal to sum of array elements Given an array of N numbers where values of the array represent memory sizes. The memory that is required by the system can only be represented in powers of 2. The task is to return the size of the memory required by the system.Examples: Input: a[] = {2, 1, 4, 5} Output: 16 The sum of memory require
6 min read
Minimum sum of two integers whose product is strictly greater than N Given an integer N, the task is to find two integers with minimum possible sum such that their product is strictly greater than N. Examples: Input: N = 10Output: 7Explanation: The integers are 3 and 4. Their product is 3 Ã 4 = 12, which is greater than N. Input: N = 1Output: 3Explanation: The intege
14 min read