Discover two pairs such that one’s GCD is identical as different’s LCM and sum equal to N

[ad_1]

Given an integer N, the duty is to seek out two pairs of optimistic integers such that the GCD of the primary pair is identical because the LCM of the second pair and the sum of the values is the same as N.

Word: If a number of outputs attainable, print any of them

Examples:

Enter: N = 9
Output: 6 1 1 1
Clarification: The GCD(6, 1) = 1 and LCM(1, 1) = 1, GCD is the same as LCM.
The sum of all numbers (6 + 1 + 1 + 1) = 9

Enter: N = 3
Output: -1
Clarification: We are able to’t give optimistic integer values to all of the 4 values.

 

Strategy: The strategy of the issue is predicated on the next commentary: 

GCD of any quantity with 1 is at all times 1 and LCM of 1 and 1 will at all times be 1. 
So, repair first variety of GCD N-3 and second as 1 and all different numbers of LCM as 1 and 1. 
So on the finish the sum of all numbers (N-3 + 1 + 1 + 1) can be N and GCD (N-3, 1) can be equal to LCM(1, 1).

Observe the steps talked about beneath to implement the commentary:

  • If N < 4, then discovering 4 such values just isn’t attainable.
  • Else discover the 4 values as per the above commentary.

Under is the code for above implementation:

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

void discover(int n)

{

    

    

    

    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;

    }

}

  

int principal()

{

    int N = 9;

  

    

    discover(9);

    return 0;

}

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

[ad_2]

Leave a Reply