Monday, August 15, 2022
HomeSoftware DevelopmentTest if Array may be generated the place no component is Geometric...

Test if Array may be generated the place no component is Geometric imply of neighbours


Given two integers P and N denoting the frequency of optimistic and adverse values, the duty is to verify when you can assemble an array utilizing P optimistic parts and N adverse parts having the identical absolute worth (i.e. when you use X, then adverse integer might be -X) such that no component is the geometric imply of its neighbours.


Enter: P = 3, N = 2
Output: True
Rationalization: it’s potential to create an array : X, X, -X, -X, X

Enter: P = 4, N = 0
Output: False


Strategy: Beneath is the commentary for the strategy:

B is alleged to be the geometric imply of A and C if B2 = A*C.
Since B2 is all the time optimistic, So, both B = X or B = -X and B2 = X2 as a result of X*X = X2 and (-X)*(-X) = X2.  

Therefore, the Predecessor and Successor have all the time reverse signal.
So the array may have a sample like {X, X, -X, -X, X, X}

Primarily based on the above commentary the answer may be derived as:

  • If the distinction between P and N is bigger than 2 then the above association isn’t potential.
  • If the distinction is strictly 2 then:
    • In the event that they happen odd instances every, the association gained’t be potential as there might be a section like {X, -X, X} or {-X, X, -X}.
    • In any other case, the association is feasible
  • If the distinction is lower than 2, then the association is all the time potential.

Beneath is the implementation of the above strategy:



#embrace <bits/stdc++.h>

#outline ll lengthy lengthy

utilizing namespace std;


bool checkGM(int P, int N)




    if (abs(P - N) >= 3)

        return false;

    if (abs(P - N) == 2) {

        if (P & 1)

            return false;


            return true;


    return true;



int essential()


    ll P = 3, N = 2;



    bool ans = checkGM(P, N);

    if (ans)

        cout << "True";


        cout << "False";

    return 0;


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



Most Popular

Recent Comments