Given a number N. Print three distinct numbers (>=1) whose product is equal to N. print -1 if it is not possible to find three numbers.
Output: 2 4 8
(2*4*8 = 64)
Output: 2 3 4
(2*3*4 = 24)
No such triplet exists
- Make an array which stores all the divisors of the given number using the approach discussed in this article
- Let the three number be a, b, c initialize to 1
- Traverse the divisors array and check the following condition:
- value of a = value at 1st index of divisor array.
- value of b = product of value at 2nd and 3rd index of divisor array. If divisor array has only one or two element then no such triplets exists
- After finding a & b, value of c = product of all the rest elements in divisor array.
- Check the final condition such that value of a, b, c must be distinct and not equal to 1.
Below is the implementation code:
2 4 8
Time Complexity: O((log N)*sqrt(N))
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check whether a number can be expressed as a product of single digit numbers
- Check if all sub-numbers have distinct Digit product
- Check if a number can be expressed as a product of exactly K prime divisors
- Check if a number can be expressed as a sum of consecutive numbers
- Check if a number can be expressed as sum two abundant numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Find two distinct prime numbers with given product
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check whether product of 'n' numbers is even or odd
- Check if product of first N natural numbers is divisible by their sum
- Check if each element of the given array is the product of exactly K prime numbers
- Check if product of array containing prime numbers is a perfect square
- Numbers within a range that can be expressed as power of two numbers
- Check if a number can be expressed as 2^x + 2^y
- Check if a number can be expressed as a^b | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.