VOOZH about

URL: https://www.geeksforgeeks.org/dsa/seeds-or-seed-roots-of-a-number/

⇱ Seeds (Or Seed Roots) of a number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Seeds (Or Seed Roots) of a number

Last Updated : 4 May, 2025

Given a positive integer n, the task is to find all possible seeds of n. If no such x exists, output -1. A number x is called a seed of n if the product of x and all its digits is equal to n. That is, x * digit1 * digit2 * ... * digitk = n, where digits are from x.
Note: The valuex should not be equal to n.

Examples:

Input: n = 138
Output: 23
Explanation: 23 is a seed because 23 * 2 * 3 = 138.

Input: n = 4977
Output: 79 711
Explanation: 79 is a seed because 79 * 7 * 9 = 4977
711 is a seed because 711 * 7 * 1 * 1 = 4977

Input: n = 11
Output: -1
Explanation: No number x (less than n), satisfies x multiplied by its digits equals 11.

[Expected Approach] Check for Every x from 1 to n/2 - O(n) Time and O(1) Space

The idea is to try all numbers less than or equal to n/2 because any valid candidate x must satisfy x * product of its digits = n. For each number in the range, we compute the product of its digits and check if it satisfies the condition. This ensures that we only collect numbers that are genuine seeds of n.

Why only till n/2?

Because if x > n/2, even if product of digits is 1 (minimum non-zero), the product x*1 > n/2, so anything larger won’t satisfy x * digitsProduct = n. Hence, checking till n/2 is sufficient and safe.


Output
23 

[Optimized Approach] Check x that are Divisors of n - O(n) Time and O(1) Space

In this approach, the idea is still the same. However, to optimize the previous approach, we observe that such an x must be a divisor of n, because if x * something equals n, then clearly n % x should be 0. So instead of trying all values from 1 to n/2, we now only check those x which divide n. This reduces unnecessary checks and improves efficiency.


Output
23 
Comment
Article Tags: