Given two integers P and N denoting the frequency of positive and negative values, the task is to check if you can construct an array using P positive elements and N negative elements having the same absolute value (i.e. if you use X, then negative integer will be -X) such that no element is the geometric mean of its neighbours.
Examples:
Input: P = 3, N = 2
Output: True
Explanation: it is possible to create an array : X, X, -X, -X, X
Input: P = 4, N = 0
Output: False
Approach: Below is the observation for the approach:
B is said to be the geometric mean of A and C if B2 = A*C.
Since B2 is always positive, So, either B = X or B = -X and B2 = X2 because X*X = X2 and (-X)*(-X) = X2.
Hence, the Predecessor and Successor have always opposite sign.
So the array will have a pattern like {X, X, -X, -X, X, X}
Based on the above observation the solution can be derived as:
- If the difference between P and N is greater than 2 then the above arrangement is not possible.
- If the difference is exactly 2 then:
- If they occur odd times each, the arrangement won’t be possible as there will be a segment like {X, -X, X} or {-X, X, -X}.
- Otherwise, the arrangement is possible
- If the difference is less than 2, then the arrangement is always possible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long
using 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 ;
else
return true ;
}
return true ;
}
int main()
{
ll P = 3, N = 2;
bool ans = checkGM(P, N);
if (ans)
cout << "True" ;
else
cout << "False" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static boolean checkGM( int P, int N)
{
if (Math.abs(P - N) >= 3 )
return false ;
if (Math.abs(P - N) == 2 ) {
if ((P & 1 ) != 0 )
return false ;
else
return true ;
}
return true ;
}
public static void main(String args[])
{
int P = 3 , N = 2 ;
boolean ans = checkGM(P, N);
if (ans)
System.out.print( "True" );
else
System.out.print( "False" );
}
}
|
Python3
def checkGM(P, N):
z = P - N
if (z < 0 ):
z = z * ( - 1 )
if (z > = 3 ):
return 0
if (z = = 2 ):
if (P & 1 ):
return 0
else :
return 1
return 1
P = 3
N = 2
ans = checkGM(P, N);
if (ans is 1 ):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
class GFG
{
static bool checkGM( int P, int N)
{
if (Math.Abs(P - N) >= 3)
return false ;
if (Math.Abs(P - N) == 2) {
if ((P & 1) != 0)
return false ;
else
return true ;
}
return true ;
}
public static void Main()
{
int P = 3, N = 2;
bool ans = checkGM(P, N);
if (ans)
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
}
}
|
Javascript
function checkGM(P, N)
{
var z = P - N;
if (z < 0)
z = z*(-1);
if (z >= 3)
return 0;
if (z == 2)
{
if (P & 1)
return 0;
else
return 1;
}
return 1;
}
var P = 3;
var N = 2;
var ans = checkGM(P, N);
if (ans == 1)
console.log( "True" );
else
console.log( "False" );
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 May, 2022
Like Article
Save Article