Determine can all number of array be made equal in C

Can all number of an array be made equal

Here, in this page we will discuss can all the number of an array be made equal in C.

Let take an array arr[]. We need to check if all the numbers of an array can be  made equal to a particular number. In a single operation, any element of the array can be either multiplied by 2 or by 3. If it’s  possible to make all the array elements equal with the given operation then print Yes else print No.

Example:

 Input: 3

 50 75 100

 Output: Yes

 Explanation :{ 50 * 2 * 3,  75 * 2 * 2, 100 * 3 } => { 300,  300,  300 }

 Input: 2

 10 14

 Output: No

can all number of array be made equal

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 :

  • Take the size of the array from the user and store it in variable say n.
  • Declare an array of size n.
  • Take n elements of the array from the user.
  • Start traversing the array and check if the number is divisible by 2.
  • If it is divisible, divide the array element by 2.
  • Similarly, check if the array element is divisible by 3.
  • If it is divisible, divide the array element by 3.
  • Then, check the remaining elements with the first element of the array.
  • If they are equal, the array can be made equal.
Can all number of array be made equal

Code in C based ob above Algorithm

#include <stdio.h>
    int make_equal(int a[], int n){
        for (int i = 0; i < n; i++) {
        // Divide number by 2
            while (a[i] % 2 == 0)
            a[i] /= 2;
            // Divide number by 3
            while (a[i] % 3 == 0)
            a[i] /= 3;
        }
        // Remaining numbers
        for (int i = 1; i < n; i++)
            if (a[i] != a[0]) {
            return 0;
            }
    return 1;
    }
    int main(){
        int a[15], n, i;
        scanf("%d", &n);
        for(i=0; i<n; i++)
        scanf("%d", &a[i]);
        if (make_equal(a, n))
        printf("Yes");
        else
        scanf("No");
        return 0;
    }
Output :

Enter the size of the array : 3

Enter the elements of the array : 50 75 100

Yes