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
- Largest triplet product in a stream
- Maximum product of a triplet (subsequence of size 3) in array
- Find N integers with given difference between product and sum
- Find a triplet in an array whose sum is closest to a given number
- Find a triplet that sum to a given value
- 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 pair with maximum product in array of Integers
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Maximum GCD of N integers with given product
- Find a triplet such that sum of two equals to third element
- Find triplet with minimum sum
- Find a triplet in an array such that arr[i] arr[k] and i < j < k
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.