Program to check if all the numbers of an array can be made equal

Select Articles

Program to check if all the numbers of an array can be made equal

Let us assume an array arr[]. We need to check if all the numbers of an array can be equalized 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 ->{50 * 2 * 3, 75 * 2 * 2, 100 * 3} = {300, 300, 300}

Input:

2

10 14

Output:

No


 

Algorithm:

To make the elements of an array all equal,

  • 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 equalized.

 

Program to check if all the numbers of an array are equal

Asked in companies like Paypal.

 

C

// C program to check if all the  numbers of an array are equal

#include

int make_equal(int a[], int n)

{

int flag = 1;

for (int i = 0; i < n>

{

// 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>

{

if (a[i] != a[0])

{

flag = 0 ;

}

return flag;

}

}

int main()

{

int n, i;

scanf(“%d”, &n);

int a[n];

for(i=0; i

scanf(“%d”, &a[i]);

if (make_equal(a, n) == 1)

printf(“Yes”);

else

printf(“No”);

return 0;

}

 

C++

// C++ program to check if all the numbers of an array are equal

#include

using namespace std;

bool make_equal(int a[], int n)

{

for (int i = 0; i < n>

// 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>

if (a[i] != a[0]) {

return false;

}

return true;

}

int main()

{

int a[15], n, i;

cin>>n;

for(i=0; i

cin>>a[i];

if (make_equal(a, n))

cout << “Yes”;

else

cout << “No”;

return 0;

}

 

JAVA

// Java program to check if all the numbers of an array are equal

import java.util.*;

public class Main

{

public static boolean make_equal(int a[], int n)

{

for (int i = 0; i < n>

{

// 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>

if (a[i] != a[0])

{

return false;

}

return true;

}

public static void main (String[] args)

{

Scanner sc = new Scanner(System.in);

int num,i;

System.out.println(“Enter the number of entries: “);

num = sc.nextInt();

int a[] = new int[num];

System.out.println(“Enter the elements: “);

for(i=0; i

{

a[i] = sc.nextInt();

}

if (make_equal(a, num))

System.out.println(“Yes”);

else

System.out.println(“No”);

}

}

 

PYTHON 3

# Python program to check if all the elements in an array are equal

def make_equal(a,n):

for i in range (n):

while (a[i]%2 == 0):

a[i] = a[i]/2

while (a[i]%3 == 0):

a[i] = a[i]/3

for i in range (n):

if a[i] != a[0]:

return False

return True

num = int(input(“Enter the number of entries: “))

arr = []

for i in range(num):

arr.append(int(input(“Enter the element:”)))

if make_equal(arr, num):

print(“Yes”)

else:

print(“No”)

 

Output

Input- 3 5 7 10 OUtput- No Input- 3 50 75 100 Output- Yes