📌  相关文章
📜  检查是否可以通过加倍或三倍使数组相等

📅  最后修改于: 2022-05-13 01:57:49.746000             🧑  作者: Mango

检查是否可以通过加倍或三倍使数组相等

给定一个包含 n 个元素的数组。您可以将数组中的元素加倍或加倍。在所有操作之后检查是否可以使数组中的所有元素相等。

例子 :

Input : A[] = {75, 150, 75, 50}
Output : Yes
Explanation : Here, 75 should be doubled twice and 
150 should be doubled once and 50 should be doubled
once and tripled once.Then, all the elements will 
be equal to 300.

Input : A[] = {100, 151, 200}
Output : No
Explanation : No matter what we do all elements in
the array could not be equal.
          
 

这个想法是重复将每个元素除以 2 和 3,直到元素可整除。在这一步之后,如果所有元素都相同,那么答案是肯定的。

这是如何运作的?我们知道每个整数都可以表示为素数2 a .3 b .5 c .7 d ..... 的乘积。因此,在我们的问题中,我们可以通过加倍(*2)或三倍(*3)来增加 a 和 b。我们可以通过乘以 2 或 3 使数组中所有元素的 a 和 b 相等。但是这些数字在它们的乘积表示中也有其他素数,我们不能改变它们的幂。因此,要使所有数字相等,它们应该从一开始就对其他素数具有相等的权力。我们可以通过将所有数字尽可能多地除以二或三来检查它。那么他们都应该是平等的。

C++
// C++ program to check if all numbers can
// be made equal by repeated division of 2
// and 3
#include 
using namespace std;
 
bool canMakeEqual(int a[], int n)
{
    for (int i = 0; i < n; i++) {
 
        // continuously divide every number by 2 
        while (a[i] % 2 == 0)
            a[i] = a[i] / 2;
 
       // continuously divide every number by 3
        while (a[i] % 3 == 0)
            a[i] = a[i] / 3;
    }
 
    // Check if all numbers same
    for (int i = 1; i < n; i++)
        if (a[i] != a[0])
           return false;
 
    return true;
}
 
// Driver Code
int main()
{
    int A[] = { 75, 150, 75, 50 };
    int n = sizeof(A) / sizeof(A[0]);
    if (canMakeEqual(A, n))
       cout << "Yes";
    else
       cout << "No";
    return 0;
}


Java
// Java program to check if all numbers can
// be made equal by repeated division of 2
// and 3
import java.util.*;
 
class GFG {
 
static Boolean canMakeEqual(int a[], int n)
{
    for (int i = 0; i < n; i++) {
 
        // Continuously divide every number by 2
        while (a[i] % 2 == 0)
            a[i] = a[i] / 2;
 
    // Continuously divide every number by 3
        while (a[i] % 3 == 0)
            a[i] = a[i] / 3;
    }
 
    // Check if all numbers same
    for (int i = 1; i < n; i++)
        if (a[i] != a[0])
        return false;
 
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
int A[] = { 75, 150, 75, 50 };
    int n = A.length;
    if (canMakeEqual(A, n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
    }
 
// This code is contributed by 'Gitanjali'.


Python3
# Python3 code to check if all numbers can
# be made equal by repeated division of 2
# and 3
 
def canMakeEqual( a , n ):
    for i in range(n):
 
        # continuously divide every number by 2
        while a[i] % 2 == 0:
            a[i] = int(a[i] / 2)
         
        # continuously divide every number by 3
        while a[i] % 3 == 0:
            a[i] = int(a[i] / 3)
     
    # Check if all numbers same
    for i in range(1,n):
        if a[i] != a[0]:
            return False
     
    return True
     
# Driver Code
A = [ 75, 150, 75, 50 ]
n = len(A)
print("Yes" if canMakeEqual(A, n) else "No")
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program to check if all numbers can
// be made equal by repeated division of 2
// and 3
using System;
 
class GFG {
 
    static Boolean canMakeEqual(int []a, int n)
    {
        for (int i = 0; i < n; i++) {
     
            // Continuously divide every number by 2
            while (a[i] % 2 == 0)
                a[i] = a[i] / 2;
     
            // Continuously divide every number by 3
            while (a[i] % 3 == 0)
                a[i] = a[i] / 3;
        }
     
        // Check if all numbers same
        for (int i = 1; i < n; i++)
            if (a[i] != a[0])
                return false;
     
        return true;
    }
     
    // Driver Code
    public static void Main()
    {
         
        int []A = { 75, 150, 75, 50 };
        int n = A.Length;
         
        if (canMakeEqual(A, n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by 'vt_m'.


PHP


Javascript


输出 :

Yes