Given a positive integer N, the task is to find a triple of three distinct positive integers (X, Y, Z) such that X + Y + Z = N and X = GCD (Y, Z).
Example:
Input: N = 12
Output:1 2 9
Explanation: The triplet (1, 2, 9) is set of distinct integers such that 1 + 2 + 9 = 12 and 1 = GCD(2, 9).
Input: N = 5675
Output:1 3 5671
Naive Approach: The basic idea is to iterate to find all possible triplets of (X, Y, Z) with sum N and for each triplet, check if GCD(Y, Z) = X.
Code-
C++
#include <bits/stdc++.h>
using namespace std;
void printTriplet( int N)
{
for ( int i=1;i<N;i++){
for ( int j=i+1;j<N;j++){
for ( int k=j+1;k<N;k++){
if (i+j+k==N && __gcd(j,k)==i){
cout<<i<< " " <<j<< " " <<k<<endl;
return ;
}
}
}
}
}
int main()
{
int N = 5875;
printTriplet(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void printTriplet( int N)
{
for ( int i = 1 ; i < N; i++) {
for ( int j = i + 1 ; j < N; j++) {
for ( int k = j + 1 ; k < N; k++) {
if (i + j + k == N && gcd(j, k) == i) {
System.out.println(i + " " + j + " "
+ k);
return ;
}
}
}
}
}
static int gcd( int a, int b)
{
if (b == 0 ) {
return a;
}
return gcd(b, a % b);
}
public static void main(String[] args)
{
int N = 5875 ;
printTriplet(N);
}
}
|
Python3
import math
def printTriplet(N):
for i in range ( 1 , N):
for j in range (i + 1 , N):
for k in range (j + 1 , N):
if i + j + k = = N and math.gcd(j, k) = = i:
print (i, j, k)
return
if __name__ = = "__main__" :
N = 5875
printTriplet(N)
|
C#
using System;
class MainClass {
static void PrintTriplet( int N)
{
for ( int i = 1; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
for ( int k = j + 1; k < N; k++) {
if (i + j + k == N && gcd(j, k) == i) {
Console.WriteLine(i + " " + j + " "
+ k);
return ;
}
}
}
}
}
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public static void Main( string [] args)
{
int N = 5875;
PrintTriplet(N);
}
}
|
Javascript
function printTriplet(N)
{
for (let i = 1; i < N; i++)
{
for (let j = i + 1; j < N; j++)
{
for (let k = j + 1; k < N; k++)
{
if (i + j + k == N && gcd(j, k) == i)
{
console.log(i, j, k);
return ;
}
}
}
}
}
function gcd(a, b) {
if (b == 0) return a;
return gcd(b, a % b);
}
let N = 5875;
printTriplet(N);
|
Time Complexity: O(N3*logN),logN for finding GCD, and N3 for three “for” loops
Auxiliary space: O(1)
Efficient Approach: The above approach can be further optimized using the observation that for any given N, there are the following three cases:
- Case 1: If N is even then, a valid triplet is (1, N/2, N/2 -1).
- Case 2: If N is odd and (N/2) is even then, a valid triplet is (1, N/2 + 1, N/2 -1).
- Case 3: If N is odd and (N/2) is also odd then, a valid triplet is (1, N/2 – 2, N/2 + 2).
Hence, for any given N, identify the case and print its respective triplet.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int printTriplet( int N)
{
if (N % 2 == 0) {
cout << 1 << " " << (N / 2)
<< " " << (N / 2) - 1;
}
else {
if ((N / 2) % 2 == 0) {
cout << 1 << " "
<< (N / 2) - 1 << " "
<< (N / 2) + 1;
}
else {
cout << 1 << " "
<< (N / 2) - 2 << " "
<< (N / 2) + 2;
}
}
}
int main()
{
int N = 5875;
printTriplet(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void printTriplet( int N)
{
if (N % 2 == 0 ) {
System.out.print( 1 + " " + (N / 2 ) +
" " + ((N / 2 ) - 1 ));
} else {
if ((N / 2 ) % 2 == 0 ) {
System.out.print( 1 + " " + ((N / 2 ) - 1 ) +
" " + ((N / 2 ) + 1 ));
}
else {
System.out.print( 1 + " " + ((N / 2 ) - 2 ) +
" " + ((N / 2 ) + 2 ));
}
}
}
public static void main(String[] args) {
int N = 5875 ;
printTriplet(N);
}
}
|
Python3
def printTriplet(N):
if (N % 2 = = 0 ):
print (f "{1} {(N / 2)} {(N / 2) - 1}" )
else :
if ((N / / 2 ) % 2 = = 0 ):
print (f "{1} {(N // 2) - 1} {(N // 2) + 1}" )
else :
print (f "{1} {(N // 2) - 2} {(N // 2) + 2}" )
if __name__ = = "__main__" :
N = 5875
printTriplet(N)
|
C#
using System;
class GFG{
static void printTriplet( int N)
{
if (N % 2 == 0)
{
Console.Write(1 + " " + (N / 2) + " " +
((N / 2) - 1));
}
else
{
if ((N / 2) % 2 == 0)
{
Console.Write(1 + " " + ((N / 2) - 1) + " " +
((N / 2) + 1));
}
else
{
Console.Write(1 + " " + ((N / 2) - 2) + " " +
((N / 2) + 2));
}
}
}
public static void Main()
{
int N = 5875;
printTriplet(N);
}
}
|
Javascript
<script>
function printTriplet(N)
{
if (N % 2 == 0) {
document.write(1 + " " + Math.floor(N / 2)
+ " " + (Math.floor(N / 2) - 1));
}
else {
if ((N / 2) % 2 == 0) {
document.write(1 + " "
+ (Math.floor(N / 2) - 1) + " "
+ (Math.floor(N / 2) + 1));
}
else {
document.write(1 + " "
+ (Math.floor(N / 2) - 2) + " "
+ (Math.floor(N / 2) + 2));
}
}
}
let N = 5875;
printTriplet(N);
</script>
|
Time Complexity: O(1)
Auxiliary space: O(1)