Given n, find x, y, z such that x, y, z satisfy the equation “2/n = 1/x + 1/y + 1/z”
There are multiple x, y and z that satisfy the equation print anyone of them, if not possible then print -1.
Examples:
Input : 3
Output : 3 4 12
Explanation: here 3 4 and 12 satisfy
the given equation
Input : 7
Output : 7 8 56
Note that for n = 1 there is no solution, and for n > 1 there is solution x = n, y = n+1, z = n·(n+1).
To come to this solution, represent 2/n as 1/n+1/n and reduce the problem to represent 1/n as a sum of two fractions. Let’s find the difference between 1/n and 1/(n+1) and get a fraction 1/(n*(n+1)), so the solution is
2/n = 1/n + 1/(n+1) + 1/(n*(n+1))
C++
#include <bits/stdc++.h>
using namespace std;
void printXYZ( int n)
{
if (n == 1)
cout << -1;
else
cout << "x is " << n << "\ny is "
<< n + 1 << "\nz is "
<< n * (n + 1);
}
int main()
{
int n = 7;
printXYZ(n);
return 0;
}
|
Java
import java.io.*;
class Sums {
static void printXYZ( int n){
if (n == 1 )
System.out.println(- 1 );
else {
System.out.println( "x is " + n);
System.out.println( "y is " + (n+ 1 ));
System.out.println( "z is " + (n * (n + 1 )));
}
}
public static void main (String[] args) {
int n = 7 ;
printXYZ(n);
}
}
|
Python3
def printXYZ( n ):
if n = = 1 :
print ( - 1 )
else :
print ( "x is " , n )
print ( "y is " ,n + 1 )
print ( "z is " ,n * (n + 1 ))
n = 7
printXYZ(n)
|
C#
using System;
class GFG
{
static void printXYZ( int n)
{
if (n == 1)
Console.WriteLine(-1);
else
{
Console.WriteLine( "x is " + n);
Console.WriteLine( "y is " + (n+1));
Console.WriteLine( "z is " + (n * (n + 1)));
}
}
public static void Main ()
{
int n = 7;
printXYZ(n);
}
}
|
PHP
<?php
function printXYZ( $n )
{
if ( $n == 1)
echo -1;
else
echo "x is " , $n , "\ny is "
, $n + 1 , "\nz is " ,
$n * ( $n + 1);
}
$n = 7;
printXYZ( $n );
?>
|
Javascript
<script>
function printXYZ(n)
{
if (n == 1)
document.write(-1);
else
document.write( "x is " + n + "<br>y is "
+ (n + 1) + "<br>z is "
+ n * (n + 1));
}
let n = 7;
printXYZ(n);
</script>
|
Output
x is 7
y is 8
z is 56
Time Complexity: O(1)
Auxiliary Space: O(1)
Alternate Solution
We can write 2/n = 1/n + 1/n. And further as 2/n = 1/n + 1/2n + 1/2n.
so here x = n, y = 2*n and z = 2*n
Below is the code implementation for the alternate solution
C++
#include <bits/stdc++.h>
using namespace std;
void printXYZ( int n)
{
if (n == 1)
cout << -1;
else
cout << "x is " << n << "\ny is " << 2 * n
<< "\nz is " << 2 * n;
}
int main()
{
int n = 7;
printXYZ(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void printXYZ( int n) {
if (n == 1 )
System.out.println( "-1" );
else
System.out.println( "x is " + n + "\ny is " + 2 * n +
"\nz is " + 2 * n);
}
public static void main(String[] args) {
int n = 7 ;
printXYZ(n);
}
}
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
static void printXYZ( int n)
{
if (n == 1)
Console.Write(-1);
else
Console.Write( "x is " + n + "\ny is " + 2 * n
+ "\nz is " + 2 * n);
}
static public void Main()
{
int n = 7;
printXYZ(n);
}
}
|
Javascript
function printXYZ( n)
{
if (n == 1)
console.log(-1);
else
console.log( "x is " + n + "\ny is " + 2 * n
+ "\nz is " + 2 * n);
}
let n = 7;
printXYZ(n);
|
Python3
def printXYZ(n):
if n = = 1 :
print ( "-1" )
else :
print (f "x is {n}\ny is {2 * n}\nz is {2 * n}" )
if __name__ = = '__main__' :
n = 7
printXYZ(n)
|
Output
x is 7
y is 14
z is 14
Time Complexity: O(1)
Auxiliary Space: O(1)
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 neveropen!