Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given a 2D integer array, queries. For each queries[i], where queries[i] = [ni, ki], find the number of different ways you can place positive integers into an array of size ni such that the product of the integers is ki. As the number of ways may be too large, the answer to the ith query is the number of ways modulo 109 + 7.
Return an integer array answer where answer.length == queries.length, and answer[i] is the answer to the ith query.
Example 1:
Input: queries = [[2,6],[5,1],[73,660]] Output: [4,1,50734910] Explanation: Each query is independent. [2,6]: There are 4 ways to fill an array of size 2 that multiply to 6: [1,6], [2,3], [3,2], [6,1]. [5,1]: There is 1 way to fill an array of size 5 that multiply to 1: [1,1,1,1,1]. [73,660]: There are 1050734917 ways to fill an array of size 73 that multiply to 660. 1050734917 modulo 109 + 7 = 50734910.
Example 2:
Input: queries = [[1,1],[2,2],[3,3],[4,4],[5,5]] Output: [1,2,3,10,5]
Constraints:
1 <= queries.length <= 104 1 <= ni, ki <= 104Problem summary: You are given a 2D integer array, queries. For each queries[i], where queries[i] = [ni, ki], find the number of different ways you can place positive integers into an array of size ni such that the product of the integers is ki. As the number of ways may be too large, the answer to the ith query is the number of ways modulo 109 + 7. Return an integer array answer where answer.length == queries.length, and answer[i] is the answer to the ith query.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Dynamic Programming
[[2,6],[5,1],[73,660]]
[[1,1],[2,2],[3,3],[4,4],[5,5]]
count-the-number-of-ideal-arrays)smallest-value-after-replacing-with-sum-of-prime-factors)closest-prime-numbers-in-range)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1735: Count Ways to Make Array With Product
class Solution {
private static final int N = 10020;
private static final int MOD = (int) 1e9 + 7;
private static final long[] F = new long[N];
private static final long[] G = new long[N];
private static final List<Integer>[] P = new List[N];
static {
F[0] = 1;
G[0] = 1;
Arrays.setAll(P, k -> new ArrayList<>());
for (int i = 1; i < N; ++i) {
F[i] = F[i - 1] * i % MOD;
G[i] = qmi(F[i], MOD - 2, MOD);
int x = i;
for (int j = 2; j <= x / j; ++j) {
if (x % j == 0) {
int cnt = 0;
while (x % j == 0) {
++cnt;
x /= j;
}
P[i].add(cnt);
}
}
if (x > 1) {
P[i].add(1);
}
}
}
public static long qmi(long a, long k, long p) {
long res = 1;
while (k != 0) {
if ((k & 1) == 1) {
res = res * a % p;
}
k >>= 1;
a = a * a % p;
}
return res;
}
public static long comb(int n, int k) {
return (F[n] * G[k] % MOD) * G[n - k] % MOD;
}
public int[] waysToFillArray(int[][] queries) {
int m = queries.length;
int[] ans = new int[m];
for (int i = 0; i < m; ++i) {
int n = queries[i][0], k = queries[i][1];
long t = 1;
for (int x : P[k]) {
t = t * comb(x + n - 1, n - 1) % MOD;
}
ans[i] = (int) t;
}
return ans;
}
}
// Accepted solution for LeetCode #1735: Count Ways to Make Array With Product
const n = 1e4 + 20
const mod = 1e9 + 7
var f = make([]int, n)
var g = make([]int, n)
var p = make([][]int, n)
func qmi(a, k, p int) int {
res := 1
for k != 0 {
if k&1 == 1 {
res = res * a % p
}
k >>= 1
a = a * a % p
}
return res
}
func init() {
f[0], g[0] = 1, 1
for i := 1; i < n; i++ {
f[i] = f[i-1] * i % mod
g[i] = qmi(f[i], mod-2, mod)
x := i
for j := 2; j <= x/j; j++ {
if x%j == 0 {
cnt := 0
for x%j == 0 {
cnt++
x /= j
}
p[i] = append(p[i], cnt)
}
}
if x > 1 {
p[i] = append(p[i], 1)
}
}
}
func comb(n, k int) int {
return (f[n] * g[k] % mod) * g[n-k] % mod
}
func waysToFillArray(queries [][]int) (ans []int) {
for _, q := range queries {
n, k := q[0], q[1]
t := 1
for _, x := range p[k] {
t = t * comb(x+n-1, n-1) % mod
}
ans = append(ans, t)
}
return
}
# Accepted solution for LeetCode #1735: Count Ways to Make Array With Product
N = 10020
MOD = 10**9 + 7
f = [1] * N
g = [1] * N
p = defaultdict(list)
for i in range(1, N):
f[i] = f[i - 1] * i % MOD
g[i] = pow(f[i], MOD - 2, MOD)
x = i
j = 2
while j <= x // j:
if x % j == 0:
cnt = 0
while x % j == 0:
cnt += 1
x //= j
p[i].append(cnt)
j += 1
if x > 1:
p[i].append(1)
def comb(n, k):
return f[n] * g[k] * g[n - k] % MOD
class Solution:
def waysToFillArray(self, queries: List[List[int]]) -> List[int]:
ans = []
for n, k in queries:
t = 1
for x in p[k]:
t = t * comb(x + n - 1, n - 1) % MOD
ans.append(t)
return ans
// Accepted solution for LeetCode #1735: Count Ways to Make Array With Product
/**
* [1735] Count Ways to Make Array With Product
*
* You are given a 2D integer array, queries. For each queries[i], where queries[i] = [ni, ki], find the number of different ways you can place positive integers into an array of size ni such that the product of the integers is ki. As the number of ways may be too large, the answer to the i^th query is the number of ways modulo 10^9 + 7.
* Return an integer array answer where answer.length == queries.length, and answer[i] is the answer to the i^th query.
*
* Example 1:
*
* Input: queries = [[2,6],[5,1],[73,660]]
* Output: [4,1,50734910]
* Explanation: Each query is independent.
* [2,6]: There are 4 ways to fill an array of size 2 that multiply to 6: [1,6], [2,3], [3,2], [6,1].
* [5,1]: There is 1 way to fill an array of size 5 that multiply to 1: [1,1,1,1,1].
* [73,660]: There are 1050734917 ways to fill an array of size 73 that multiply to 660. 1050734917 modulo 10^9 + 7 = 50734910.
*
* Example 2:
*
* Input: queries = [[1,1],[2,2],[3,3],[4,4],[5,5]]
* Output: [1,2,3,10,5]
*
*
* Constraints:
*
* 1 <= queries.length <= 10^4
* 1 <= ni, ki <= 10^4
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/count-ways-to-make-array-with-product/
// discuss: https://leetcode.com/problems/count-ways-to-make-array-with-product/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn ways_to_fill_array(queries: Vec<Vec<i32>>) -> Vec<i32> {
vec![]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1735_example_1() {
let queries = vec![vec![2, 6], vec![5, 1], vec![73, 660]];
let result = vec![4, 1, 50734910];
assert_eq!(Solution::ways_to_fill_array(queries), result);
}
#[test]
#[ignore]
fn test_1735_example_2() {
let queries = vec![vec![1, 1], vec![2, 2], vec![3, 3], vec![4, 4], vec![5, 5]];
let result = vec![1, 2, 3, 10, 5];
assert_eq!(Solution::ways_to_fill_array(queries), result);
}
}
// Accepted solution for LeetCode #1735: Count Ways to Make Array With Product
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1735: Count Ways to Make Array With Product
// class Solution {
// private static final int N = 10020;
// private static final int MOD = (int) 1e9 + 7;
// private static final long[] F = new long[N];
// private static final long[] G = new long[N];
// private static final List<Integer>[] P = new List[N];
//
// static {
// F[0] = 1;
// G[0] = 1;
// Arrays.setAll(P, k -> new ArrayList<>());
// for (int i = 1; i < N; ++i) {
// F[i] = F[i - 1] * i % MOD;
// G[i] = qmi(F[i], MOD - 2, MOD);
// int x = i;
// for (int j = 2; j <= x / j; ++j) {
// if (x % j == 0) {
// int cnt = 0;
// while (x % j == 0) {
// ++cnt;
// x /= j;
// }
// P[i].add(cnt);
// }
// }
// if (x > 1) {
// P[i].add(1);
// }
// }
// }
//
// public static long qmi(long a, long k, long p) {
// long res = 1;
// while (k != 0) {
// if ((k & 1) == 1) {
// res = res * a % p;
// }
// k >>= 1;
// a = a * a % p;
// }
// return res;
// }
//
// public static long comb(int n, int k) {
// return (F[n] * G[k] % MOD) * G[n - k] % MOD;
// }
//
// public int[] waysToFillArray(int[][] queries) {
// int m = queries.length;
// int[] ans = new int[m];
// for (int i = 0; i < m; ++i) {
// int n = queries[i][0], k = queries[i][1];
// long t = 1;
// for (int x : P[k]) {
// t = t * comb(x + n - 1, n - 1) % MOD;
// }
// ans[i] = (int) t;
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.