Check if all the elements can be made equal on dividing with X and Y
Last Updated :
31 May, 2022
Given an array arr[] and two integers X and Y. The task is to check whether it is possible to make all the elements equal by dividing them with X and Y any number of times including 0.
Examples:
Input: arr[] = {2, 4, 6, 8}, X = 2, Y = 3
Output: Yes
2 -> 2
4 -> (4 / X) = (4 / 2) = 2
6 -> (6 / Y) = (6 / 3) = 2
8 -> (8 / X) = (8 / 2) = 4 and 4 -> (4 / X) = (4 / 2) = 2
Input: arr[] = {2, 4, 10}, X = 11, Y = 12
Output: No
Approach: Find the gcd of all the elements from the given array because this gcd is the value which can we get by dividing all the elements with some arbitrary constants say gcd = arr[0] / k1 or arr[1] / k2 or … or arr[n-1] / kn. Now the task is to find whether these constants k1, k2, k3, …, kn are of the form X * X * X * … * Y Y Y * ….. If yes then it is possible to make all the elements equal with the given operation else it isn’t.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisible( int num, int x, int y)
{
while (num % x == 0 || num % y == 0) {
if (num % x == 0)
num /= x;
if (num % y == 0)
num /= y;
}
if (num > 1)
return false ;
return true ;
}
bool isPossible( int arr[], int n, int x, int y)
{
int gcd = arr[0];
for ( int i = 1; i < n; i++)
gcd = __gcd(gcd, arr[i]);
for ( int i = 0; i < n; i++) {
if (!isDivisible(arr[i] / gcd, x, y))
return false ;
}
return true ;
}
int main()
{
int arr[] = { 2, 4, 6, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 2, y = 3;
if (isPossible(arr, n, x, y))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
public static boolean isDivisible( int num, int x, int y)
{
while (num % x == 0 || num % y == 0 )
{
if (num % x == 0 )
num /= x;
if (num % y == 0 )
num /= y;
}
if (num > 1 )
return false ;
return true ;
}
public static int _gcd( int a, int b)
{
while (a != b)
{
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
public static boolean isPossible( int [] arr, int n,
int x, int y)
{
int gcd = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
gcd = _gcd(gcd, arr[i]);
for ( int i = 0 ; i < n; i++)
{
if (!isDivisible(arr[i] / gcd, x, y))
return false ;
}
return true ;
}
public static void main(String[] args)
{
int [] arr = { 2 , 4 , 6 , 8 };
int n = arr.length;
int x = 2 , y = 3 ;
if (isPossible(arr, n, x, y))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
from math import gcd as __gcd
def isDivisible(num, x, y):
while (num % x = = 0 or num % y = = 0 ):
if (num % x = = 0 ):
num / / = x
if (num % y = = 0 ):
num / / = y
if (num > 1 ):
return False
return True
def isPossible(arr, n, x, y):
gcd = arr[ 0 ]
for i in range ( 1 ,n):
gcd = __gcd(gcd, arr[i])
for i in range (n):
if (isDivisible(arr[i] / / gcd, x, y) = = False ):
return False
return True
arr = [ 2 , 4 , 6 , 8 ]
n = len (arr)
x = 2
y = 3
if (isPossible(arr, n, x, y) = = True ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
public static bool isDivisible( int num, int x, int y)
{
while (num % x == 0 || num % y == 0)
{
if (num % x == 0)
num /= x;
if (num % y == 0)
num /= y;
}
if (num > 1)
return false ;
return true ;
}
public static int _gcd( int a, int b)
{
while (a != b)
{
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
public static bool isPossible( int [] arr, int n,
int x, int y)
{
int gcd = arr[0];
for ( int i = 1; i < n; i++)
gcd = _gcd(gcd, arr[i]);
for ( int i = 0; i < n; i++)
{
if (!isDivisible(arr[i] / gcd, x, y))
return false ;
}
return true ;
}
public static void Main()
{
int [] arr = { 2, 4, 6, 8 };
int n = arr.Length;
int x = 2, y = 3;
if (isPossible(arr, n, x, y))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isDivisible(num, x, y)
{
while (num % x == 0 || num % y == 0) {
if (num % x == 0)
num /= x;
if (num % y == 0)
num /= y;
}
if (num > 1)
return false ;
return true ;
}
function __gcd(a, b)
{
while (a != b)
{
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
function isPossible(arr, n, x, y)
{
var gcd = arr[0];
for ( var i = 1; i < n; i++)
gcd = __gcd(gcd, arr[i]);
for ( var i = 0; i < n; i++) {
if (!isDivisible(arr[i] / gcd, x, y))
return false ;
}
return true ;
}
var arr = [ 2, 4, 6, 8 ];
var n = arr.length;
var x = 2, y = 3;
if (isPossible(arr, n, x, y))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n*log(max(x,y))), where n , x , y are given by the user
Auxiliary Space: O(1), as no extra space is used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...