VOOZH about

URL: https://www.geeksforgeeks.org/dsa/birthday-gift-counting-integer-arrays-with-interesting-properties/

⇱ Birthday Gift: Counting integer Arrays with interesting properties - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Birthday Gift: Counting integer Arrays with interesting properties

Last Updated : 11 Mar, 2026

Your birthday is coming soon and one of your friends, Alex, is thinking about a gift for you. He knows that you really like integer arrays with interesting properties.

  • He selected two numbers, N and K, and decided to write down on paper all integer arrays of length K (in the form a[1], a[2], …, a[K]), where every number a[i] is in the range 1 to N. Moreover, a[i+1] must be divisible by a[i] (for 1 ≤ i < K).
  • Alex is very patient, so he managed to do this. Now you need to find how many different arrays are written on the paper. Since the answer can be very large, print the result modulo 10000.

Example:

Input: N = 3, K = 2
Output: 5
Explanation: All possible arrays are: [1, 1], [1, 2], [1, 3], [2, 2], [3, 3].

Naïve Solution - O(n^k) Time and O(1) Space

Generate all possible arrays of length K with elements from 1 to N, check if a[i+1] % a[i] = 0 for 1 ≤ i < K, and count the arrays that satisfy this condition.

Efficient Approach (Dynamic Programming) - O(n^2 * log(k)) Time and O(1) Space

  • Use a DP table where dp[i][j] represents the number of valid arrays of length i ending with value j.
  • Initialize the base case: for arrays of length 1, every number from 1 to N can appear once.
  • For each length from 2 to K, update the table by adding values from previous elements that divide the current element.
  • Finally, sum all values in dp[K][j] for 1 ≤ j ≤ N and return the result modulo 10000.

Output
5
Comment
Article Tags: