Given an integer X, the task is to find the three distinct integers greater than 1 i.e. A, B and C such that (A * B * C) = X. If no such triplet exists then print -1.
Input: X = 64
Output: 2 4 8
(2 * 4 * 8) = 64
Input: X = 32
No such triplet exists.
Approach: Suppose we have a triplet (A, B, C). Notice that, for their product to be equal to X, each of the integer has to be a factor of X. So, store all the factors of X in O(sqrt(X)) time using the approach discussed in this article.
There will be at most sqrt(X) factors now. Next, iterate on each factor by running two loops, one picking A and another picking B. Now if this triplet is valid i.e. C = X / (A * B) where C is also a factor of X. To check that, store all the factors in an unordered_set. If a valid triplet is found then print the triplet else print -1.
Below is the implementation of the above approach:
3 5 7
- Minimum product of k integers in an array of positive Integers
- Maximum product of a triplet (subsequence of size 3) in array
- Largest triplet product in a stream
- Find a triplet that sum to a given value
- Find a triplet in an array whose sum is closest to a given number
- Find N integers with given difference between product and sum
- Find any K distinct odd integers such that their sum is equal to N
- Find K distinct positive odd integers with sum N
- Find N distinct integers with zero sum
- Find N distinct integers with sum N
- Find two distinct prime numbers with given product
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Find a triplet such that sum of two equals to third element
- Find triplet with minimum sum
- Pythagorean Triplet with given sum
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Triplet with a given sum in BST | Set 2
- Pythagorean Triplet with given sum using single loop
- Largest lexicographic triplet from a given Array that forms a triangle
- Minimum possible sum of prices of a Triplet from the given Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.