给定一个整数数组和一个质数数组。任务是找到是否可以通过将素数给定素数数组中的一个或多个元素相乘来使整数数组的所有元素相等。
例子:
Input : arr[] = {50, 200}
prime[] = {2, 3}
Output : Yes
We can multiply 50 with 2 two times
to make both elements of arr[] equal
Input : arr[] = {3, 4, 5, 6, 2}
prime[] = {2, 3}
Output : No
我们找到所有数组元素的LCM。仅当可以将所有数字转换为LCM时,才能使所有元素相等。因此,我们找到每个元素的乘数,以便可以通过将该数字乘以使该元素等于LCM。之后,我们发现给定质数的数字是否可以形成给定的乘数。
算法-
步骤1:查找数组O(n)中所有数字的LCM
第2步:对于每个数字arr [i]
——–将LCM除以arr [i]
——–使用每个输入质数除以结果以除去输入质数的所有因子(可以使用模来检查除数)
—如果剩余数字不为1,则返回false;否则,返回false。
步骤3:返回true
下面是上述算法的实现。
C++
// C++ program to find if array elements can
// be made same
#include
using namespace std;
// To calculate LCM of whole array
int lcmOfArray(int arr[], int n)
{
int ans = arr[0];
for (int i=1; i
Java
// Java program to find if array
// elements can be made same
class GFG
{
static int ___gcd(int a, int b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return ___gcd(a - b, b);
return ___gcd(a, b - a);
}
// To calculate LCM of whole array
static int lcmOfArray(int arr[], int n)
{
int ans = arr[0];
for (int i = 1; i < n; i++)
ans = (arr[i] * ans)/ ___gcd(arr[i], ans);
return ans;
}
// function to check possibility if we can make
// all element same or not
static boolean checkArray(int arr[], int prime[],
int n, int m)
{
// Find LCM of whole array
int lcm = lcmOfArray(arr,n);
// One by one check if value of lcm / arr[i]
// can be formed using prime numbers.
for (int i = 0; i < n; i++)
{
// divide each element of array by LCM
int val = lcm / arr[i];
// Use each input prime number to divide
// the result to remove all factors of
// input prime numbers
for (int j = 0; j < m && val != 1; j++)
while (val % prime[j] == 0)
val = val / prime[j];
// If the remaining value is not 1, then
// it is not possible to make all elements
// same.
if (val != 1)
return false;
}
return true;
}
// Driver code
public static void main (String[] args)
{
int arr[] = {50, 200};
int prime[] = {2, 3};
int n = arr.length;
int m = prime.length;
if(checkArray(arr, prime, n, m))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to find
# if array elements can
# be made same
def ___gcd(a,b):
# Everything divides 0
if (a == 0 or b == 0):
return 0
# base case
if (a == b):
return a
# a is greater
if (a > b):
return ___gcd(a-b, b)
return ___gcd(a, b-a)
# To calculate LCM of whole array
def lcmOfArray(arr,n):
ans = arr[0]
for i in range(1,n):
ans = (arr[i]*ans)/___gcd(arr[i], ans)
return ans
# function to check possibility
# if we can make
# all element same or not
def checkArray(arr, prime, n, m):
# Find LCM of whole array
lcm = lcmOfArray(arr, n)
# One by one check if
# value of lcm / arr[i]
# can be formed using prime numbers.
for i in range(n):
# divide each element
# of array by LCM
val = lcm/arr[i]
# Use each input prime
# number to divide
# the result to remove
# all factors of
# input prime numbers
for j in range(m and val!=1):
while (val % prime[j] == 0):
val = val/prime[j]
# If the remaining value
# is not 1, then
# it is not possible to
# make all elements
# same.
if (val != 1):
return 0
return 1
# Driver code
arr = [50, 200]
prime = [2, 3]
n = len(arr)
m = len(prime)
if(checkArray(arr, prime, n, m)):
print("Yes")
else:
print("No")
# This code is contributed
# by Anant Agarwal.
C#
// C# program to find if array
// elements can be made same
using System;
class GFG {
static int ___gcd(int a, int b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return ___gcd(a - b, b);
return ___gcd(a, b - a);
}
// To calculate LCM of whole array
static int lcmOfArray(int []arr, int n)
{
int ans = arr[0];
for (int i = 1; i < n; i++)
ans = ((arr[i] * ans) /
___gcd(arr[i], ans));
return ans;
}
// function to check possibility if
// we can make all element same or not
static bool checkArray(int []arr,
int []prime, int n, int m)
{
// Find LCM of whole array
int lcm = lcmOfArray(arr, n);
// One by one check if value of
// lcm / arr[i] can be formed
// using prime numbers.
for (int i = 0; i < n; i++)
{
// divide each element of
// array by LCM
int val = lcm / arr[i];
// Use each input prime number
// to divide the result to
// remove all factors of
// input prime numbers
for (int j = 0; j < m &&
val != 1; j++)
while (val % prime[j] == 0)
val = val / prime[j];
// If the remaining value is not 1,
// then it is not possible to make
// all elements same.
if (val != 1)
return false;
}
return true;
}
// Driver code
public static void Main ()
{
int []arr = {50, 200};
int []prime = {2, 3};
int n = arr.Length;
int m = prime.Length;
if(checkArray(arr, prime, n, m))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by nitin mittal
PHP
$b)
return ___gcd($a - $b, $b);
return ___gcd($a, $b - $a);
}
// To calculate LCM
// of whole array
function lcmOfArray($arr, $n)
{
$ans = $arr[0];
for ($i = 1; $i < $n; $i++)
$ans = (($arr[$i] * $ans) /
___gcd($arr[$i], $ans));
return $ans;
}
// function to check
// possibility if we
// can make all element
// same or not
function checkArray($arr, $prime,
$n, $m)
{
// Find LCM of
// whole array
$lcm = lcmOfArray($arr, $n);
// One by one check if
// value of lcm / arr[i]
// can be formed using
// prime numbers.
for ($i = 0; $i < $n; $i++)
{
// divide each element
// of array by LCM
$val = $lcm / $arr[$i];
// Use each input prime
// number to divide the
// result to remove all
// factors of input prime
// numbers
for ($j = 0; $j < $m &&
$val != 1; $j++)
while ($val % $prime[$j] == 0)
$val = $val / $prime[$j];
// If the remaining value
// is not 1, then it is
// not possible to make
// all elements same.
if ($val != 1)
return false;
}
return true;
}
// Driver code
$arr = array(50, 200);
$prime = array(2, 3);
$n = sizeof($arr);
$m = sizeof($prime);
if(checkArray($arr, $prime,
$n, $m))
echo "Yes";
else
echo "No";
// This code is contributed
// by akt_mit
?>
Javascript
输出:
Yes