Seeds (Or Seed Roots) of a number (original) (raw)

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.

Try It Yourselfredirect icon

Table of Content

[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.

C++ `

// C++ program to find all seeds of a number // by checking for every x from 1 to n/2 #include <bits/stdc++.h> using namespace std;

// Function to get product of digits // of a number int getDigitProduct(int x) {

int prod = 1;

while (x > 0) {
    int digit = x % 10;

    // If any digit is 0, product
    // becomes 0
    if (digit == 0) {
        return 0;
    }

    prod *= digit;
    x /= 10;
}

return prod;

}

// Function to return all seeds of n vector seeds(int n) {

vector<int> res;

// Check every number from 1 to n/2
for (int i = 1; i <= n / 2; i++) {

    // Multiply i with product of its digits
    if (i * getDigitProduct(i) == n) {
        res.push_back(i);
    }
}

// If no seeds found, return -1
if (res.empty()) {
    res.push_back(-1);
}

return res;

}

// Driver code int main() {

int n = 138;

vector<int> ans = seeds(n);

for (int x : ans) {
    cout << x << " ";
}

cout << endl;

return 0;

}

Java

// Java program to find all seeds of a number // by checking for every x from 1 to n/2 import java.util.*;

class GfG {

// Function to get product of digits
// of a number
static int getDigitProduct(int x) {

    int prod = 1;

    while (x > 0) {
        int digit = x % 10;

        // If any digit is 0, product
        // becomes 0
        if (digit == 0) {
            return 0;
        }

        prod *= digit;
        x /= 10;
    }

    return prod;
}

// Function to return all seeds of n
static ArrayList<Integer> seeds(int n) {

    ArrayList<Integer> res = new ArrayList<>();

    // Check every number from 1 to n/2
    for (int i = 1; i <= n / 2; i++) {

        // Multiply i with product of its digits
        if (i * getDigitProduct(i) == n) {
            res.add(i);
        }
    }

    // If no seeds found, return -1
    if (res.isEmpty()) {
        res.add(-1);
    }

    return res;
}

// Driver code
public static void main(String[] args) {

    int n = 138;

    ArrayList<Integer> ans = seeds(n);

    for (int x : ans) {
        System.out.print(x + " ");
    }

    System.out.println();
}

}

Python

Python program to find all seeds of a number

by checking for every x from 1 to n/2

Function to get product of digits

of a number

def getDigitProduct(x):

prod = 1

while x > 0:
    digit = x % 10

    # If any digit is 0, product
    # becomes 0
    if digit == 0:
        return 0

    prod *= digit
    x //= 10

return prod

Function to return all seeds of n

def seeds(n):

res = []

# Check every number from 1 to n/2
for i in range(1, n // 2 + 1):

    # Multiply i with product of its digits
    if i * getDigitProduct(i) == n:
        res.append(i)

# If no seeds found, return -1
if not res:
    res.append(-1)

return res

Driver code

if name == "main":

n = 138

ans = seeds(n)

for x in ans:
    print(x, end=" ")

print()

C#

// C# program to find all seeds of a number // by checking for every x from 1 to n/2 using System; using System.Collections.Generic;

class GfG {

// Function to get product of digits
// of a number
static int getDigitProduct(int x) {

    int prod = 1;

    while (x > 0) {
        int digit = x % 10;

        // If any digit is 0, product
        // becomes 0
        if (digit == 0) {
            return 0;
        }

        prod *= digit;
        x /= 10;
    }

    return prod;
}

// Function to return all seeds of n
static List<int> seeds(int n) {

    List<int> res = new List<int>();

    // Check every number from 1 to n/2
    for (int i = 1; i <= n / 2; i++) {

        // Multiply i with product of its digits
        if (i * getDigitProduct(i) == n) {
            res.Add(i);
        }
    }

    // If no seeds found, return -1
    if (res.Count == 0) {
        res.Add(-1);
    }

    return res;
}

// Driver code
public static void Main() {

    int n = 138;

    List<int> ans = seeds(n);

    foreach (int x in ans) {
        Console.Write(x + " ");
    }

    Console.WriteLine();
}

}

JavaScript

// JavaScript program to find all seeds of a number // by checking for every x from 1 to n/2

// Function to get product of digits // of a number function getDigitProduct(x) {

let prod = 1;

while (x > 0) {
    let digit = x % 10;

    // If any digit is 0, product
    // becomes 0
    if (digit === 0) {
        return 0;
    }

    prod *= digit;
    x = Math.floor(x / 10);
}

return prod;

}

// Function to return all seeds of n function seeds(n) {

let res = [];

// Check every number from 1 to n/2
for (let i = 1; i <= Math.floor(n / 2); i++) {

    // Multiply i with product of its digits
    if (i * getDigitProduct(i) === n) {
        res.push(i);
    }
}

// If no seeds found, return -1
if (res.length === 0) {
    res.push(-1);
}

return res;

}

// Driver code let n = 138;

let ans = seeds(n);

for (let x of ans) { process.stdout.write(x + " "); }

console.log();

`

[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.

C+ `

// C++ program to find all seeds of a number // by checking only those x that are divisor of n #include <bits/stdc++.h> using namespace std;

// Function to get product of digits // of a number int getDigitProduct(int x) {

int prod = 1;

while (x > 0) {
    int digit = x % 10;

    // If any digit is 0, product
    // becomes 0
    if (digit == 0) {
        return 0;
    }

    prod *= digit;
    x /= 10;
}

return prod;

}

// Function to return all seeds of n vector seeds(int n) {

vector<int> res;

// Check every number from 1 to n/2
for (int i = 1; i <= n / 2; i++) {
     
    // if n is not divisible by i, skip
    if(n % i != 0) continue;
    
    // Multiply i with product of its digits
    if (i * getDigitProduct(i) == n) {
        res.push_back(i);
    }
}

// If no seeds found, return -1
if (res.empty()) {
    res.push_back(-1);
}

return res;

}

// Driver code int main() {

int n = 138;

vector<int> ans = seeds(n);

for (int x : ans) {
    cout << x << " ";
}

cout << endl;

return 0;

}

Java

// Java program to find all seeds of a number // by checking only those x that are divisor of n import java.util.*;

class GfG {

// Function to get product of digits
// of a number
static int getDigitProduct(int x) {

    int prod = 1;

    while (x > 0) {
        int digit = x % 10;

        // If any digit is 0, product
        // becomes 0
        if (digit == 0) {
            return 0;
        }

        prod *= digit;
        x /= 10;
    }

    return prod;
}

// Function to return all seeds of n
static int[] seeds(int n) {

    ArrayList<Integer> res = new ArrayList<>();

    // Check every number from 1 to n/2
    for (int i = 1; i <= n / 2; i++) {
        
        // if n is not divisible by i, skip
        if (n % i != 0) continue;

        // Multiply i with product of its digits
        if (i * getDigitProduct(i) == n) {
            res.add(i);
        }
    }

    // If no seeds found, return -1
    if (res.isEmpty()) {
        return new int[]{-1};
    }

    // Convert to array
    int[] arr = new int[res.size()];
    for (int i = 0; i < res.size(); i++) {
        arr[i] = res.get(i);
    }

    return arr;
}

// Driver code
public static void main(String[] args) {

    int n = 138;

    int[] ans = seeds(n);

    for (int x : ans) {
        System.out.print(x + " ");
    }

    System.out.println();
}

}

Python

Python program to find all seeds of a number

by checking only those x that are divisor of n

Function to get product of digits

of a number

def getDigitProduct(x):

prod = 1

while x > 0:
    digit = x % 10

    # If any digit is 0, product
    # becomes 0
    if digit == 0:
        return 0

    prod *= digit
    x //= 10

return prod

Function to return all seeds of n

def seeds(n):

res = []

# Check every number from 1 to n/2
for i in range(1, n // 2 + 1):

    # if n is not divisible by i, skip
    if n % i != 0:
        continue

    # Multiply i with product of its digits
    if i * getDigitProduct(i) == n:
        res.append(i)

# If no seeds found, return -1
if not res:
    res.append(-1)

return res

Driver code

if name == "main":

n = 138

ans = seeds(n)

for x in ans:
    print(x, end=" ")

print()

C#

// C# program to find all seeds of a number // by checking only those x that are divisor of n using System; using System.Collections.Generic;

class GfG {

// Function to get product of digits
// of a number
static int getDigitProduct(int x) {

    int prod = 1;

    while (x > 0) {
        int digit = x % 10;

        // If any digit is 0, product
        // becomes 0
        if (digit == 0) {
            return 0;
        }

        prod *= digit;
        x /= 10;
    }

    return prod;
}

// Function to return all seeds of n
static int[] seeds(int n) {

    List<int> res = new List<int>();

    // Check every number from 1 to n/2
    for (int i = 1; i <= n / 2; i++) {
        
        // if n is not divisible by i, skip
        if (n % i != 0) continue;

        // Multiply i with product of its digits
        if (i * getDigitProduct(i) == n) {
            res.Add(i);
        }
    }

    // If no seeds found, return -1
    if (res.Count == 0) {
        return new int[]{-1};
    }

    return res.ToArray();
}

// Driver code
static void Main() {

    int n = 138;

    int[] ans = seeds(n);

    foreach (int x in ans) {
        Console.Write(x + " ");
    }

    Console.WriteLine();
}

}

JavaScript

// JavaScript program to find all seeds of a number // by checking only those x that are divisor of n

// Function to get product of digits // of a number function getDigitProduct(x) {

let prod = 1;

while (x > 0) {
    let digit = x % 10;

    // If any digit is 0, product
    // becomes 0
    if (digit === 0) {
        return 0;
    }

    prod *= digit;
    x = Math.floor(x / 10);
}

return prod;

}

// Function to return all seeds of n function seeds(n) {

let res = [];

// Check every number from 1 to n/2
for (let i = 1; i <= Math.floor(n / 2); i++) {

    // if n is not divisible by i, skip
    if (n % i !== 0) continue;

    // Multiply i with product of its digits
    if (i * getDigitProduct(i) === n) {
        res.push(i);
    }
}

// If no seeds found, return -1
if (res.length === 0) {
    res.push(-1);
}

return res;

}

// Driver code let n = 138;

let ans = seeds(n);

for (let x of ans) { process.stdout.write(x + " "); }

console.log();

`