Count the Number of Ideal Arrays - Problem
Count the Number of Ideal Arrays

You are given two integers n and maxValue, and your task is to find the number of ideal arrays of length n.

An ideal array is a 0-indexed integer array arr of length n that satisfies these conditions:
โ€ข Every element arr[i] is between 1 and maxValue (inclusive)
โ€ข Each element is divisible by the previous element: arr[i] % arr[i-1] == 0 for all i > 0

In other words, the array must be non-decreasing and each element must be a multiple of the previous element.

Example: If n=3, maxValue=5, then [1,2,4] is ideal because 2%1==0 and 4%2==0.

Return the count modulo 109 + 7 since the answer can be very large.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 2, maxValue = 5
โ€บ Output: 10
๐Ÿ’ก Note: The ideal arrays are: [1,1], [1,2], [1,3], [1,4], [1,5], [2,2], [2,4], [3,3], [4,4], [5,5]. Each array satisfies the divisibility condition.
example_2.py โ€” Length 3
$ Input: n = 3, maxValue = 4
โ€บ Output: 11
๐Ÿ’ก Note: Valid arrays include [1,1,1], [1,1,2], [1,1,3], [1,1,4], [1,2,2], [1,2,4], [2,2,2], [2,2,4], [2,4,4], [3,3,3], [4,4,4].
example_3.py โ€” Edge Case
$ Input: n = 1, maxValue = 3
โ€บ Output: 3
๐Ÿ’ก Note: With length 1, any single value from 1 to maxValue is valid: [1], [2], [3].

Visualization

Tap to expand
Building Ideal Arrays (n=3, maxValue=4)111[1,1,1]112[1,1,2]122[1,2,2]124[1,2,4]224[2,2,4]Divisibility Rules:โœ“ 1 divides everything (1, 2, 3, 4)โœ“ 2 divides (2, 4)โœ“ 3 divides (3)โœ“ 4 divides (4)DP Transition:dp[i][j] = ฮฃ dp[i-1][k] where k|jSum over all divisors k of jTotal Arrays: 112|4 โœ“2|4 โœ“
Understanding the Visualization
1
Start with Base
Place any block from 1 to maxValue as the foundation
2
Add Valid Multiples
On each level, only place blocks that are multiples of the one below
3
Count Possibilities
Use DP to count all possible towers of height n
4
Sum Results
Add up counts for all possible ending values
Key Takeaway
๐ŸŽฏ Key Insight: An ideal array is essentially a sequence where each element is a multiple of the previous one. We can solve this efficiently using dynamic programming by counting the number of ways to build arrays of each length ending with each possible value.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(maxValue * log(maxValue) + n * P)

Where P is the number of primes up to maxValue. Sieve takes O(maxValue * log(log(maxValue))), DP takes O(n * number of distinct prime factor patterns)

n
2n
โšก Linearithmic
Space Complexity
O(maxValue + n * P)

Store prime factorization for each number plus DP table

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค n โ‰ค 104
  • 1 โ‰ค maxValue โ‰ค 104
  • Return the result modulo 109 + 7
  • Each arr[i] must be divisible by arr[i-1]
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
28.4K Views
Medium Frequency
~35 min Avg. Time
892 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