给定一个数组arr[] ,任务是使所有数组元素与给定的操作相等。
在单个操作中,数组的任何元素都可以乘以3或5任意次。如果可以使所有数组元素与给定的操作相等,则打印是,否则打印否。
例子:
Input: arr[] = {18, 30, 54, 90, 162}
Output: Yes
Explanation:
We can perform following operations:
162 X 5 = 810
90 X 3 X 3 = 810
54 X 5 X 3 = 810
30 X 3 X 3 X 3 = 810
18 X 5 X 3 X 3 = 810
Input: arr[] = {18, 36, 58, 90, 162}
Output: No
Explanation:
There is no way you can make all elements equal.
观察:
- 如果经过一些操作后,所有的数字都相等,那么它们将具有相同的质数因式分解,即每个数字将具有相同的 2、3、5 ……的幂,依此类推。
- 由于我们仅将数字乘以质数3和5 ,因此我们可以在某些运算后使所有数字的质因数分解中的3 和 5 的幂相等。
- 因此,要使所有数字相等,除3和5之外的素数分解中的素数的幂必须相等。
- 解决方案是取每个数字并从中删除3和5 的所有幂。如果所有数字都相等,则可以通过使用给定的操作使数组元素相等,否则不可能。
步骤:
- 将数组arr[] 的每个元素除以 3 和 5,使得每个元素的质因数分解中3和5 的所有幂都为零。
- 检查数组的所有元素是否相等。如果是,则打印是。
- 否则打印编号
下面是上述方法的实现:
CPP
// C++ implementation to find if it's
// possible to make all elements of an
// array equal by using two operations.
#include
using namespace std;
// Function to find if it's possible
// to make all array elements equal
bool canMakeEqual(int a[], int n)
{
// Iterate over all numbers
for (int i = 0; i < n; i++) {
// If a number has a power of 5
// remove it
while (a[i] % 5 == 0) {
a[i] /= 5;
}
// If a number has a power of 3
// remove it
while (a[i] % 3 == 0) {
a[i] /= 3;
}
}
int last = a[0];
// Check if all elements are equal
// in the final array
for (int i = 1; i < n; i++) {
if (a[i] != last) {
return false;
}
}
return true;
}
// Driver's Code
int main()
{
int arr[] = { 18, 30, 54, 90, 162 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call to check if all
// element in the array can be equal
// or not.
if (canMakeEqual(arr, n)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
return 0;
}
Java
// Java implementation to find if it's
// possible to make all elements of an
// array equal by using two operations.
class GFG{
// Function to find if it's possible
// to make all array elements equal
static boolean canMakeEqual(int a[], int n)
{
// Iterate over all numbers
for (int i = 0; i < n; i++) {
// If a number has a power of 5
// remove it
while (a[i] % 5 == 0) {
a[i] /= 5;
}
// If a number has a power of 3
// remove it
while (a[i] % 3 == 0) {
a[i] /= 3;
}
}
int last = a[0];
// Check if all elements are equal
// in the final array
for (int i = 1; i < n; i++) {
if (a[i] != last) {
return false;
}
}
return true;
}
// Driver's Code
public static void main(String[] args)
{
int arr[] = { 18, 30, 54, 90, 162 };
int n = arr.length;
// Function call to check if all
// element in the array can be equal
// or not.
if (canMakeEqual(arr, n)) {
System.out.print("YES" +"\n");
}
else {
System.out.print("NO" +"\n");
}
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation to find if it's
# possible to make all elements of an
# array equal by using two operations.
# Function to find if it's possible
# to make all array elements equal
def canMakeEqual( a, n) :
# Iterate over all numbers
for i in range(n) :
# If a number has a power of 5
# remove it
while (a[i] % 5 == 0) :
a[i] //= 5;
# If a number has a power of 3
# remove it
while (a[i] % 3 == 0) :
a[i] //= 3;
last = a[0];
# Check if all elements are equal
# in the final array
for i in range(1,n) :
if (a[i] != last) :
return False;
return True;
# Driver's Code
if __name__ == "__main__" :
arr = [ 18, 30, 54, 90, 162 ];
n = len(arr);
# Function call to check if all
# element in the array can be equal
# or not.
if (canMakeEqual(arr, n)) :
print("YES");
else :
print("NO");
# This code is contributed by AnkitRai01
C#
// C# implementation to find if it's
// possible to make all elements of an
// array equal by using two operations.
using System;
class GFG{
// Function to find if it's possible
// to make all array elements equal
static bool canMakeEqual(int []a, int n)
{
// Iterate over all numbers
for (int i = 0; i < n; i++) {
// If a number has a power of 5
// remove it
while (a[i] % 5 == 0) {
a[i] /= 5;
}
// If a number has a power of 3
// remove it
while (a[i] % 3 == 0) {
a[i] /= 3;
}
}
int last = a[0];
// Check if all elements are equal
// in the final array
for (int i = 1; i < n; i++) {
if (a[i] != last) {
return false;
}
}
return true;
}
// Driver's Code
public static void Main(string[] args)
{
int []arr = { 18, 30, 54, 90, 162 };
int n = arr.Length;
// Function call to check if all
// element in the array can be equal
// or not.
if (canMakeEqual(arr, n)) {
Console.WriteLine("YES");
}
else {
Console.WriteLine("NO");
}
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
YES
时间复杂度: O(N),其中 N 是数组的大小。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live