702685. Minimum element whose N-th power is greater than product of an array

Easy

Minimum Element Whose N-th Power is Greater than Product of an Array

Summary

Given an array arr and an integer n, find the minimum element x in the array such that x^n > prod(arr), where prod(arr) is the product of all elements in the array. This problem involves mathematical concepts, arrays, and algorithms.

Detailed Explanation


To solve this problem, we can iterate through the array and check each element to see if its nth power is greater than the product of the entire array. We keep track of the minimum such element as we go along. Here's a step-by-step breakdown:

  1. Initialize a variable min_x to infinity (or any large number).
  2. Iterate through each element x in the array.
  3. Calculate x^n.
  4. Check if x^n > prod(arr). If it is, update min_x with the current value of x.
  5. Return min_x.

Time complexity: O(n), where n is the size of the input array.

Space complexity: O(1), since we only use a constant amount of space to store min_x and other variables.

Here's an ASCII art diagram illustrating the algorithm:

          +---------------+
          |  Iterate through |
          |  each element x  |
          +---------------+
                  |
                  | Check if x^n > prod(arr)
                  v
+-----------------------+
| Update min_x with x    |
+-----------------------+

Optimized Solutions


Java

View on GeeksforGeeks
public int findMinElement(int[] arr, int n) {
    int min_x = Integer.MAX_VALUE;
    for (int x : arr) {
        if (Math.pow(x, n) > prod(arr)) {
            min_x = Math.min(min_x, x);
        }
    }
    return min_x;
}

private long prod(int[] arr) {
    long prod = 1;
    for (int x : arr) {
        prod *= x;
    }
    return prod;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.