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))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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 sum two abundant numbers
- Check if a number can be expressed as a sum of consecutive 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
- N expressed as sum of 4 prime numbers
- Check if a number N can be expressed in base B
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check if an integer can be expressed as a sum of two semi-primes
- Check if a number can be expressed as x^y (x raised to power y)
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.