Check if Array can be generated where no element is Geometric mean of neighbours
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, XInput: 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++
// C++ code to implement the above approach #include <bits/stdc++.h> #define ll long long using namespace std; // Function to check if it is possible // to create the array or not bool checkGM( int P, int N) { // Conditions to check if it is possible // to generate the array if ( abs (P - N) >= 3) return false ; if ( abs (P - N) == 2) { if (P & 1) return false ; else return true ; } return true ; } // Driver Code int main() { ll P = 3, N = 2; // Function call bool ans = checkGM(P, N); if (ans) cout << "True" ; else cout << "False" ; return 0; } |
Java
// Java program for the above approach import java.io.*; import java.util.*; class GFG { // Function to check if it is possible // to create the array or not static boolean checkGM( int P, int N) { // Conditions to check if it is possible // to generate the array 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 ; } // Driver Code public static void main(String args[]) { int P = 3 , N = 2 ; // Function call boolean ans = checkGM(P, N); if (ans) System.out.print( "True" ); else System.out.print( "False" ); } } // This code is contributed by sanjoy_62. |
Python3
# Python3 code to implement the above approach # Function to check if it is possible # to create the array or not def checkGM(P, N): # Conditions to check if it is possible # to generate the array 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 # Driver Code P = 3 N = 2 # Function call ans = checkGM(P, N); if (ans is 1 ): print ( "True" ) else : print ( "False" ) # This code is contributed by ashishsingh13122000. |
C#
// C# program to implement // the above approach using System; class GFG { // Function to check if it is possible // to create the array or not static bool checkGM( int P, int N) { // Conditions to check if it is possible // to generate the array 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 ; } // Driver Code public static void Main() { int P = 3, N = 2; // Function call bool ans = checkGM(P, N); if (ans) Console.WriteLine( "True" ); else Console.WriteLine( "False" ); } } // This code is contributed by avijitmondal1998. |
Javascript
// JavaScript code to implement the above approach // Function to check if it is possible // to create the array or not function checkGM(P, N) { // Conditions to check if it is possible // to generate the array 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; } // Driver Code var P = 3; var N = 2; // Function call var ans = checkGM(P, N); if (ans == 1) console.log( "True" ); else console.log( "False" ); // This code is contributed by phasing17. |
True
Time Complexity: O(1)
Auxiliary Space: O(1)