Skip to content
Related Articles

Related Articles

Find two pairs such that one’s GCD is same as other’s LCM and sum equal to N

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 28 Apr, 2022

Given an integer N, the task is to find two pairs of positive integers such that the GCD of the first pair is the same as the LCM of the second pair and the sum of the values is equal to N.

Note: If multiple outputs possible, print any of them

Examples:

Input: N = 9
Output: 6 1 1 1
Explanation: The GCD(6, 1) = 1 and LCM(1, 1) = 1, GCD is equal to LCM.
The sum of all numbers (6 + 1 + 1 + 1) = 9

Input: N = 3
Output: -1
Explanation: We can’t give positive integer values to all the four values.

 

Approach: The approach of the problem is based on the following observation: 

GCD of any number with 1 is always 1 and LCM of 1 and 1 will always be 1. 
So, fix first number of GCD N-3 and second as 1 and all other numbers of LCM as 1 and 1. 
So at the end the sum of all numbers (N-3 + 1 + 1 + 1) will be N and GCD (N-3, 1) will be equal to LCM(1, 1).

Follow the steps mentioned below to implement the observation:

  • If N < 4, then finding four such values is not possible.
  • Else find the four values as per the above observation.

Below is the code for above implementation:

C




// C code for above implementation
#include <stdio.h>
 
// Function to find the pairs whose GCD and
// LCM are equal and their sum equal to N
void find(int n)
{
    // If there are several possible answers
    // you can output any of them. If not
    // exist then print -1
    if (n < 4) {
        printf("-1\n");
    }
    else {
        int a, b, c, d;
        a = n - 3;
        b = 1;
        c = 1;
        d = 1;
        printf("%d %d %d %d",a,b,c,d);
    }
}
 
// Driver code
void main()
{
    int N = 9;
 
    // Function call
    find(9);
}
 
// This code is contributed by ashishsingh13122000

C++




// C++ code for above implementation
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the pairs whose GCD and
// LCM are equal and their sum equal to N
void find(int n)
{
    // If there are several possible answers
    // you can output any of them. If not
    // exist then print -1
    if (n < 4) {
        cout << -1 << endl;
    }
    else {
        int a, b, c, d;
        a = n - 3;
        b = 1;
        c = 1;
        d = 1;
        cout << a << " " << b << " "
             << c << " " << d;
    }
}
 
// Driver code
int main()
{
    int N = 9;
 
    // Function call
    find(9);
    return 0;
}

Java




// Java code for above implementation
import java.io.*;
 
class GFG
{
 
  // Function to find the pairs whose GCD and
  // LCM are equal and their sum equal to N
  public static void find(int n)
  {
 
    // If there are several possible answers
    // you can output any of them. If not
    // exist then print -1
    if (n < 4) {
      System.out.println(-1);
    }
    else {
      int a, b, c, d;
      a = n - 3;
      b = 1;
      c = 1;
      d = 1;
      System.out.print(a + " " + b + " " + c + " "
                       + d);
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 9;
 
    // Function call
    find(9);
  }
}
 
// This code is contributed by Rohit Pradhan

Python3




# Python 3 code for above implementation
 
# Function to find the pairs whose GCD and
# LCM are equal and their sum equal to N
def find(n):
   
    # If there are several possible answers
    # you can output any of them. If not
    # exist then print -1
      if (n < 4):
        print("-1",end=" ")
      else:
        a = n - 3
        b = 1
        c = 1
        d = 1
        print(a, end=" ")
        print(b, end=" ")
        print(c, end=" ")
        print(d, end=" ")
                
# Driver code
N = 9
 
# Function call
find(9)
 
# This code is contributed by ashishsingh13122000.

C#




// C# code for above implementation
using System;
 
class GFG
{
 
  // Function to find the pairs whose GCD and
  // LCM are equal and their sum equal to N
  static void find(int n)
  {
 
    // If there are several possible answers
    // you can output any of them. If not
    // exist then print -1
    if (n < 4) {
      Console.WriteLine(-1);
    }
    else {
      int a, b, c, d;
      a = n - 3;
      b = 1;
      c = 1;
      d = 1;
      Console.Write(a + " " + b + " " + c + " " + d);
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 9;
 
    // Function call
    find(9);
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript




<script>
 
// JavaScript code for above implementation
 
// Function to find the pairs whose GCD and
// LCM are equal and their sum equal to N
function find(n){
   
    // If there are several possible answers
    // you can output any of them. If not
    // exist then print -1
    if (n < 4)
        document.write("-1"," ")
    else{
        let a = n - 3
        let b = 1
        let c = 1
        let d = 1
        document.write(a," ")
        document.write(b," ")
        document.write(c," ")
        document.write(d," ")
    }
}
                
// Driver code
let N = 9
 
// Function call
find(9)
 
// This code is contributed by shinjanpatra
 
</script>

Output

6 1 1 1

Time Complexity: O(1)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!