Count the Number of Ideal Arrays - Problem

You are given two integers n and maxValue, which are used to describe an ideal array.

A 0-indexed integer array arr of length n is considered ideal if the following conditions hold:

  • Every arr[i] is a value from 1 to maxValue, for 0 <= i < n.
  • Every arr[i] is divisible by arr[i - 1], for 0 < i < n.

Return the number of distinct ideal arrays of length n. Since the answer may be very large, return it modulo 10^9 + 7.

Input & Output

Example 1 — Basic Case
$ Input: n = 2, maxValue = 3
Output: 5
💡 Note: The 5 ideal arrays are: [1,1], [1,2], [1,3], [2,2], [3,3]. Each array satisfies: all elements ≤ 3, and each element divides the next.
Example 2 — Longer Array
$ Input: n = 3, maxValue = 2
Output: 4
💡 Note: The 4 ideal arrays are: [1,1,1], [1,1,2], [1,2,2], [2,2,2]. All arrays have length 3 with elements ≤ 2.
Example 3 — Single Element
$ Input: n = 1, maxValue = 4
Output: 4
💡 Note: With length 1, any single value is ideal: [1], [2], [3], [4]. No divisibility constraint applies.

Constraints

  • 2 ≤ n ≤ 104
  • 1 ≤ maxValue ≤ 104

Visualization

Tap to expand
Ideal Arrays: n=2, maxValue=3Each element must divide the next element[1,1][1,2][1,3][2,2][3,3]1|1 ✓1|2 ✓1|3 ✓2|2 ✓3|3 ✓[2,1][2,3][3,1]2∤1 ✗2∤3 ✗3∤1 ✗Result: 5 valid ideal arrays
Understanding the Visualization
1
Input
Given n=2, maxValue=3
2
Generate
Find all arrays where each element divides the next
3
Count
Return total count: 5 ideal arrays
Key Takeaway
🎯 Key Insight: Ideal arrays follow divisibility chains, making this a combinatorial counting problem with prime factorization
Asked in
Google 15 Meta 8 Amazon 5
25.0K Views
Medium Frequency
~45 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen