Problem solution · Java

Count Ways to Make Array With Product

Count Ways to Make Array With Product: a Java solution using hash-based lookup. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Hash-based lookup
Source
walkccc LeetCode Solutions
Length
68 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Hash-based lookup

For Count Ways to Make Array With Product, the implementation stores previously seen values or frequencies in a hash table for direct membership and lookup operations.

  1. Decide the key that represents the information needed later.
  2. Update its count or stored state while scanning the input.
  3. Use constant-time expected lookups to detect matches or assemble the result.

Code notes

  • 68 lines of Java from the credited upstream file 1735.java.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
  • 8 loop blocks detected.

Complexity

Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeCount Ways to Make Array With Product · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int[] waysToFillArray(int[][] queries) {    final int MAX = 10_000;    final int MAX_FREQ = 13; // 2^13 = 8192 < MAX    final int[] minPrimeFactors = sieveEratosthenes(MAX + 1);    final long[][] factAndInvFact = getFactAndInvFact(MAX + MAX_FREQ - 1);    final long[] fact = factAndInvFact[0];    final long[] invFact = factAndInvFact[1];    int[] ans = new int[queries.length];     for (int i = 0; i < queries.length; ++i) {      final int n = queries[i][0];      final int k = queries[i][1];      int res = 1;      for (final int freq : getPrimeFactorsCount(k, minPrimeFactors).values())        res = (int) ((long) res * nCk(n - 1 + freq, freq, fact, invFact) % MOD);      ans[i] = res;    }     return ans;  }   private static final int MOD = 1_000_000_007;   // Gets the minimum prime factor of i, where 1 < i <= n.  private int[] sieveEratosthenes(int n) {    int[] minPrimeFactors = new int[n + 1];    for (int i = 2; i <= n; ++i)      minPrimeFactors[i] = i;    for (int i = 2; i * i < n; ++i)      if (minPrimeFactors[i] == i) // `i` is prime.        for (int j = i * i; j < n; j += i)          minPrimeFactors[j] = Math.min(minPrimeFactors[j], i);    return minPrimeFactors;  }   private Map<Integer, Integer> getPrimeFactorsCount(int num, int[] minPrimeFactors) {    Map<Integer, Integer> count = new HashMap<>();    while (num > 1) {      final int divisor = minPrimeFactors[num];      while (num % divisor == 0) {        num /= divisor;        count.put(divisor, count.merge(divisor, 1, Integer::sum));      }    }    return count;  }   private long[][] getFactAndInvFact(int n) {    long[] fact = new long[n + 1];    long[] invFact = new long[n + 1];    long[] inv = new long[n + 1];    fact[0] = invFact[0] = 1;    inv[0] = inv[1] = 1;    for (int i = 1; i <= n; ++i) {      if (i >= 2)        inv[i] = MOD - MOD / i * inv[MOD % i] % MOD;      fact[i] = fact[i - 1] * i % MOD;      invFact[i] = invFact[i - 1] * inv[i] % MOD;    }    return new long[][] {fact, invFact};  }   private int nCk(int n, int k, long[] fact, long[] invFact) {    return (int) (fact[n] * invFact[k] % MOD * invFact[n - k] % MOD);  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗