Find a triplet (X, Y, Z) with given sum as N and GCD of two numbers is the third number
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: 2 4 6
Explanation: The triplet (2, 4, 6) is set of distinct integers such that 2 + 4 + 6 = 12 and 2 = GCD(4, 6).Input: N = 5675
Output:1 2835 2839
Naive Approach: The basic idea is to iterate over all possible triplets of (X, Y, Z) with sum N and for each triplet, check if GCD(Y, Z) = X.
Time Complexity: O(N2)
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++
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Function to find a triplet (X, Y, Z) // of distinct integers with their sum // as N and GCD(Y, Z) = X int printTriplet( int N) { // Case 1 where N is even if (N % 2 == 0) { cout << 1 << " " << (N / 2) << " " << (N / 2) - 1; } else { // Case 2 where N is Odd // and N/2 is even if ((N / 2) % 2 == 0) { cout << 1 << " " << (N / 2) - 1 << " " << (N / 2) + 1; } // Case 3 where N is Odd // and N/2 is also odd else { cout << 1 << " " << (N / 2) - 2 << " " << (N / 2) + 2; } } } // Driver Code int main() { int N = 5875; printTriplet(N); return 0; } |
Java
// Java program of the above approach import java.util.*; class GFG { // Function to find a triplet (X, Y, Z) // of distinct integers with their sum // as N and GCD(Y, Z) = X static void printTriplet( int N) { // Case 1 where N is even if (N % 2 == 0 ) { System.out.print( 1 + " " + (N / 2 ) + " " + ((N / 2 ) - 1 )); } else { // Case 2 where N is Odd // and N/2 is even if ((N / 2 ) % 2 == 0 ) { System.out.print( 1 + " " + ((N / 2 ) - 1 ) + " " + ((N / 2 ) + 1 )); } // Case 3 where N is Odd // and N/2 is also odd else { System.out.print( 1 + " " + ((N / 2 ) - 2 ) + " " + ((N / 2 ) + 2 )); } } } // Driver Code public static void main(String[] args) { int N = 5875 ; printTriplet(N); } } // This code is contributed by 29AjayKumar |
Python3
# python3 program of the above approach # Function to find a triplet (X, Y, Z) # of distinct integers with their sum # as N and GCD(Y, Z) = X def printTriplet(N): # Case 1 where N is even if (N % 2 = = 0 ): print (f "{1} {(N / 2)} {(N / 2) - 1}" ) else : # Case 2 where N is Odd # and N/2 is even if ((N / / 2 ) % 2 = = 0 ): print (f "{1} {(N // 2) - 1} {(N // 2) + 1}" ) # Case 3 where N is Odd # and N/2 is also odd else : print (f "{1} {(N // 2) - 2} {(N // 2) + 2}" ) # Driver Code if __name__ = = "__main__" : N = 5875 printTriplet(N) # This code is contributed by rakeshsahni |
C#
// C# program of the above approach using System; class GFG{ // Function to find a triplet (X, Y, Z) // of distinct integers with their sum // as N and GCD(Y, Z) = X static void printTriplet( int N) { // Case 1 where N is even if (N % 2 == 0) { Console.Write(1 + " " + (N / 2) + " " + ((N / 2) - 1)); } else { // Case 2 where N is Odd // and N/2 is even if ((N / 2) % 2 == 0) { Console.Write(1 + " " + ((N / 2) - 1) + " " + ((N / 2) + 1)); } // Case 3 where N is Odd // and N/2 is also odd else { Console.Write(1 + " " + ((N / 2) - 2) + " " + ((N / 2) + 2)); } } } // Driver Code public static void Main() { int N = 5875; printTriplet(N); } } // This code is contributed by ukasp |
Javascript
<script> // JavaScript code for the above approach // Function to find a triplet (X, Y, Z) // of distinct integers with their sum // as N and GCD(Y, Z) = X function printTriplet(N) { // Case 1 where N is even if (N % 2 == 0) { document.write(1 + " " + Math.floor(N / 2) + " " + (Math.floor(N / 2) - 1)); } else { // Case 2 where N is Odd // and N/2 is even if ((N / 2) % 2 == 0) { document.write(1 + " " + (Math.floor(N / 2) - 1) + " " + (Math.floor(N / 2) + 1)); } // Case 3 where N is Odd // and N/2 is also odd else { document.write(1 + " " + (Math.floor(N / 2) - 2) + " " + (Math.floor(N / 2) + 2)); } } } // Driver Code let N = 5875; printTriplet(N); // This code is contributed by Potta Lokesh </script> |
1 2935 2939
Time Complexity: O(1)
Auxiliary space: O(1)