Determine can all numbers of an array be made equal in Python

Can all numbers of an array be made equal

In this article we will see a program to check if all numbers of array can be made equal.

Consider an array arr[], the task is to make all the array elements equal with the given operation. In a single operation, any element of the array can be either multiplied by 2 or by 3. If it is possible to make all the array elements equal with the given operation then print Yes else print No.

  • Example 1: Array-[50,75,100]
    • 50 – 50*2*3=300,75 – 75*2*2=300,100 – 100*3=300
    • Elements of an array can be made equal
  • Example 2: Array-[10,14]
    • Elements of an array cannot be made equal

Now let see a program to check if all numbers of array can be made equal.

can all elements of array be equal in python

Keypoint

In this section we will learn about basic knowledge which we need to know before coding the above Program. So we must have knowledge of what is an array? 

What is an array?
An array is a data structure, it is collection of similar data elements which is stored at contiguous memory locations in which each data element can be accessed directly by only using its index number.

Algorithm:

1. Start span the array and check if the number is divisible by 2
2. If it is divisible, divide the array element by 2
3. Similarly, check if the array element is divisible by 3
4. If it is divisible, divide the array element by 3.
5. Then, check the remaining elements with the first element of the array.
6. If they are equal, the array can be made equal.
can all elements of array be equal

Python Program

#Function to check whether an array is equal or not
def check(array,length): for i in range(0, length): # Divide number by 2 while array[i] % 2 == 0: array[i] //= 2 # Divide number by 3 while array[i] % 3 == 0: array[i] //= 3 if array[i] != array[0]: return False return True #input array from user
array = list(map(int,input("Enter elements followed by space: ").split()))
#determine length of array and assign to length variable
length=len(array) if check(array, length): print("Yes, all the elements of an array can be made equal") else: print("No, all the elements of an array cannot be made equal")

Output

Enter elements followed by space: 50 75 100
Yes, all the elements of an array can be made equal