- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find minimum value to assign all array elements so that array product becomes greater in C++

Suppose we have an array of n elements. Update all elements of the given array to some min value x, such that arr[i] = x. Such that product of all elements in the new array is strictly greater than the product of all elements of the initial array, where i <= arr[i] <= 10^10, and 1 <= n <= 10^5. So if the array is like [4, 2, 1, 10, 6]. So 4 is the smallest element. 4 * 4 * 4 * 4 * 4 > 4 * 2 * 1 * 10 * 6

As we know that the product of n elements is P. If we have to find nth root of P, to find the nth root of product, we simply divide n from sum of log of n elements of the array and then the ceiling of antilog will be the result.

res = ceil(antilog(log(x) / 10))

or res = ceil(10 ^ (log(x) / 10))

## Example

#include <iostream> #include <cmath> #define EPS 1e-15 using namespace std; long long findMinValue(long long arr[], long long n) { long double sum = 0; for (int i=0; i<n; i++) sum += (long double)log10(arr[i])+EPS; long double xl = (long double)(sum/n+EPS); long double res = pow((long double)10.0, (long double)xl) + EPS; return (long long)ceil(res+EPS); } int main() { long long arr[] = {4, 2, 1, 10, 6}; long long n = sizeof(arr)/sizeof(arr[0]); cout << "Min value is: "<< findMinValue(arr, n); }

## Output

Min value is: 4

- Related Articles
- Find the minimum value to be added so that array becomes balanced in C++
- Add minimum number to an array so that the sum becomes even in C++?
- Add minimum number to an array so that the sum becomes even in C programming
- Find all elements in array which have at-least two greater elements in C++
- Elements to be added so that all elements of a range are present in array in C++
- Minimum operation to make all elements equal in array in C++
- Minimum delete operations to make all elements of array same in C++.
- Minimum operations required to make all the array elements equal in C++
- Adding elements of an array until every element becomes greater than or equal to k in C++.
- Minimum removals from array to make GCD Greater in C++
- Minimum Moves to Equal Array Elements in C++
- C++ program to Adding elements of an array until every element becomes greater than or equal to k
- Mask array elements greater than a given value in Numpy
- Find closest greater value for every element in array in C++
- Recursive Programs to find Minimum and Maximum elements of array in C++

Advertisements