Find distinct integers for a triplet with given product
Last Updated :
08 Feb, 2022
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.
Examples:
Input: X = 64
Output: 2 4 8
(2 * 4 * 8) = 64
Input: X = 32
Output: -1
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:
C++
#include <bits/stdc++.h>
using namespace std;
void findTriplets( int x)
{
vector< int > fact;
unordered_set< int > factors;
for ( int i = 2; i <= sqrt (x); i++) {
if (x % i == 0) {
fact.push_back(i);
if (x / i != i)
fact.push_back(x / i);
factors.insert(i);
factors.insert(x / i);
}
}
bool found = false ;
int k = fact.size();
for ( int i = 0; i < k; i++) {
int a = fact[i];
for ( int j = 0; j < k; j++) {
int b = fact[j];
if ((a != b) && (x % (a * b) == 0)
&& (x / (a * b) != a)
&& (x / (a * b) != b)
&& (x / (a * b) != 1)) {
cout << a << " " << b << " "
<< (x / (a * b));
found = true ;
break ;
}
}
if (found)
break ;
}
if (!found)
cout << "-1" ;
}
int main()
{
int x = 105;
findTriplets(x);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findTriplets( int x)
{
Vector<Integer> fact = new Vector<Integer>();
HashSet<Integer> factors = new HashSet<Integer>();
for ( int i = 2 ; i <= Math.sqrt(x); i++)
{
if (x % i == 0 )
{
fact.add(i);
if (x / i != i)
fact.add(x / i);
factors.add(i);
factors.add(x / i);
}
}
boolean found = false ;
int k = fact.size();
for ( int i = 0 ; i < k; i++)
{
int a = fact.get(i);
for ( int j = 0 ; j < k; j++)
{
int b = fact.get(j);
if ((a != b) && (x % (a * b) == 0 )
&& (x / (a * b) != a)
&& (x / (a * b) != b)
&& (x / (a * b) != 1 ))
{
System.out.print(a+ " " + b + " "
+ (x / (a * b)));
found = true ;
break ;
}
}
if (found)
break ;
}
if (!found)
System.out.print( "-1" );
}
public static void main(String[] args)
{
int x = 105 ;
findTriplets(x);
}
}
|
Python3
from math import sqrt
def findTriplets(x) :
fact = [];
factors = set ();
for i in range ( 2 , int (sqrt(x))) :
if (x % i = = 0 ) :
fact.append(i);
if (x / i ! = i) :
fact.append(x / / i);
factors.add(i);
factors.add(x / / i);
found = False ;
k = len (fact);
for i in range (k) :
a = fact[i];
for j in range (k) :
b = fact[j];
if ((a ! = b) and (x % (a * b) = = 0 )
and (x / (a * b) ! = a)
and (x / (a * b) ! = b)
and (x / (a * b) ! = 1 )) :
print (a,b,x / / (a * b));
found = True ;
break ;
if (found) :
break ;
if ( not found) :
print ( "-1" );
if __name__ = = "__main__" :
x = 105 ;
findTriplets(x);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findTriplets( int x)
{
List< int > fact = new List< int >();
HashSet< int > factors = new HashSet< int >();
for ( int i = 2; i <= Math.Sqrt(x); i++)
{
if (x % i == 0)
{
fact.Add(i);
if (x / i != i)
fact.Add(x / i);
factors.Add(i);
factors.Add(x / i);
}
}
bool found = false ;
int k = fact.Count;
for ( int i = 0; i < k; i++)
{
int a = fact[i];
for ( int j = 0; j < k; j++)
{
int b = fact[j];
if ((a != b) && (x % (a * b) == 0)
&& (x / (a * b) != a)
&& (x / (a * b) != b)
&& (x / (a * b) != 1))
{
Console.Write(a+ " " + b + " "
+ (x / (a * b)));
found = true ;
break ;
}
}
if (found)
break ;
}
if (!found)
Console.Write( "-1" );
}
public static void Main(String[] args)
{
int x = 105;
findTriplets(x);
}
}
|
Javascript
<script>
function findTriplets(x)
{
let fact = [];
let factors = new Set();
for (let i = 2; i <= Math.sqrt(x); i++)
{
if (x % i == 0)
{
fact.push(i);
if (x / i != i)
fact.push(x / i);
factors.add(i);
factors.add(x / i);
}
}
let found = false ;
let k = fact.length;
for (let i = 0; i < k; i++)
{
let a = fact[i];
for (let j = 0; j < k; j++)
{
let b = fact[j];
if ((a != b) && (x % (a * b) == 0)
&& (x / (a * b) != a)
&& (x / (a * b) != b)
&& (x / (a * b) != 1))
{
document.write(a+ " " + b + " "
+ (x / (a * b)));
found = true ;
break ;
}
}
if (found)
break ;
}
if (!found)
document.write( "-1" );
}
let x = 105;
findTriplets(x);
</script>
|
Time Complexity: O(N), N=X
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...