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.Examples:
Input: 64 Output: 2 4 8 Explanation: (2*4*8 = 64)Input: 24 Output: 2 3 4 Explanation: (2*3*4 = 24)Input: 12 Output: -1 Explanation: No such triplet exists
Approach:
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 elements 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:
CPP
#include "bits/stdc++.h"
using namespace std;
void getnumbers(int n)
{
vector<int> divisor;
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
divisor.push_back(i);
n /= i;
}
}
if (n != 1) {
divisor.push_back(n);
}
int a, b, c, size;
a = b = c = 1;
size = divisor.size();
for (int i = 0; i < size; i++) {
if (a == 1) {
a = a * divisor[i];
}
else if (b == 1 || b == a) {
b = b * divisor[i];
}
else {
c = c * divisor[i];
}
}
if (a == 1 || b == 1 || c == 1
|| a == b || b == c || a == c) {
cout << "-1" << endl;
}
else {
cout << a << ' ' << b
<< ' ' << c << endl;
}
}
int main()
{
int n = 64;
getnumbers(n);
}
Java
import java.util.*;
class GFG{
static void getnumbers(int n)
{
Vector<Integer> divisor = new Vector<Integer>();
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
divisor.add(i);
n /= i;
}
}
if (n != 1) {
divisor.add(n);
}
int a, b, c, size;
a = b = c = 1;
size = divisor.size();
for (int i = 0; i < size; i++) {
if (a == 1) {
a = a * divisor.get(i);
}
else if (b == 1 || b == a) {
b = b * divisor.get(i);
}
else {
c = c * divisor.get(i);
}
}
if (a == 1 || b == 1 || c == 1
|| a == b || b == c || a == c) {
System.out.print("-1" +"\n");
}
else {
System.out.print(a +" "+ b
+" "+ c +"\n");
}
}
public static void main(String[] args)
{
int n = 64;
getnumbers(n);
}
}
Python3
def getnumbers(n):
divisor = []
for i in range(2, n + 1):
while (n % i == 0):
divisor.append(i)
n //= i
if (n != 1):
divisor.append(n)
a, b, c, size = 0, 0, 0, 0
a = b = c = 1
size = len(divisor)
for i in range(size):
if (a == 1):
a = a * divisor[i]
elif (b == 1 or b == a):
b = b * divisor[i]
else:
c = c * divisor[i]
if (a == 1 or b == 1 or c == 1
or a == b or b == c or a == c):
print("-1")
else:
print(a, b, c)
n = 64
getnumbers(n)
C#
using System;
using System.Collections.Generic;
class GFG{
static void getnumbers(int n)
{
List<int> divisor = new List<int>();
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
divisor.Add(i);
n /= i;
}
}
if (n != 1) {
divisor.Add(n);
}
int a, b, c, size;
a = b = c = 1;
size = divisor.Count;
for (int i = 0; i < size; i++) {
if (a == 1) {
a = a * divisor[i];
}
else if (b == 1 || b == a) {
b = b * divisor[i];
}
else {
c = c * divisor[i];
}
}
if (a == 1 || b == 1 || c == 1
|| a == b || b == c || a == c) {
Console.Write("-1" +"\n");
}
else {
Console.Write(a +" "+ b
+" "+ c +"\n");
}
}
public static void Main(String[] args)
{
int n = 64;
getnumbers(n);
}
}
Javascript
<script>
function getnumbers(n)
{
let divisor = [];
for (let i = 2; i * i <= n; i++) {
while (n % i == 0) {
divisor.push(i);
n = Math.floor(n/i);
}
}
if (n != 1) {
divisor.push(n);
}
let a, b, c, size;
a = b = c = 1;
size = divisor.length;
for (let i = 0; i < size; i++) {
if (a == 1) {
a = a * divisor[i];
}
else if (b == 1 || b == a) {
b = b * divisor[i];
}
else {
c = c * divisor[i];
}
}
if (a == 1 || b == 1 || c == 1
|| a == b || b == c || a == c) {
document.write("-1" +"<br>");
}
else {
document.write(a +" "+ b
+" "+ c +"<br>");
}
}
let n = 64;
getnumbers(n);
</script>
Time Complexity: O((log N)*sqrt(N) )
Auxiliary Space: O(sqrt(n))
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...