Description
Check whether the numbers of array be made equal or not
For eg, for the following input it should print yes because
50*2*3 , 75*2*2 and 150*2 are equal to 300 in all cases. So array numbers can be made equal
Input
3
50 75 150
Output
Yes
Solution
C Program
#include <stdio.h>
int convert(int b[], int s)
{
int flag = 1;
for (int i = 0; i < s; i++)
{
while (b[i] % 2 == 0)
b[i] /= 2;
while (b[i] % 3 == 0)
b[i] /= 3;
}
for (int i = 1; i < s; i++)
{
if (b[i] != b[0])
{
flag = 0 ;
}
return flag;
}
}
int main()
{
int n, i;
scanf("%d", &n);
int a[n];
for(i=0; i<n; i++)
scanf("%d", &a[i]);
if (convert(a, n) == 1)
printf("Yes, possible");
else
printf("No, it's not possible");
return 0;
}
C++ Program
#include <bits/stdc++.h>
using namespace std;
int convert(int b[], int s)
{
int flag = 1;
for (int i = 0; i < s; i++)
{
while (b[i] % 2 == 0)
b[i] /= 2;
while (b[i] % 3 == 0)
b[i] /= 3;
}
for (int i = 1; i < s; i++)
{
if (b[i] != b[0])
{
flag = 0 ;
}
return flag;
}
}
int main()
{
int n, i;
cin>>n;
int a[n];
for(i=0; i<n; i++)
cin>>a[i];
if (convert(a, n) == 1)
cout<<"Yes, possible";
else
cout<<"No, it's not possible";
return 0;
}
Java Program
import java.util.*;
public class Main
{
public static boolean convert(int b[], int n)
{
for (int i = 0; i < n; i++)
{
while (b[i] % 2 == 0)
b[i] /= 2;
while (b[i] % 3 == 0)
b[i] /= 3;
}
for (int i = 1; i < n; i++)
if (b[i] != b[0])
{
return false;
}
return true;
}
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int n,i;
System.out.println("Enter the number of elements in array: ");
n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter elements: ");
for(i=0; i<n; i++)
{
a[i] = sc.nextInt();
}
if (convert(a, n))
System.out.println("Yes, possible");
else
System.out.println("No, it's not possible");
}
}
Python
def convert(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
n = int(input("Enter the number of elements in array "))
arr = []
print("Enter elements: ")
for i in range(n):
arr.append(int(input()))
if convert(arr, n):
print("Yes, possible")
else:
print("No, it's not possible")