## Adobe Interview Question

Backend Developers**Country:**India

**Interview Type:**Written Test

Take each number and divide it by every prime until you can, you will find a base number. If this base number is equal for all the numbers in the array then it is possible make all the numbers of the array equal, otherwise not. find the basic code below. I will soon upload an optimized version on

minimalcodes.wordpress.com

```
int baseNumber;
vector<int> numbers, primes; //vectors containing primes and numbers
bool start = true, possible = true;
for(int i = 0; i < numbers.size(); i++){
int number = numbers.at(i);
for(int j = 0; j < primes.size(); j++){
while(number % primes.at(j) == 0){ //&& number != 1
number = number / primes.at(j);
}
if(number == 1){
break;
}
}
if(start){
start = false;
baseNumber = number;
} else if (baseNumber != number){
possible = false;
break;
}
}
cout << "My method : " << possible << endl;
```

```
public class ArrayTest {
public static void main(String[] args) {
int[] arr = {3,15};
int[] primes = {5};
System.out.println(checkEquality(arr, primes));
}
public static boolean checkEquality(int[] arr,int[] primes){
int max = arr[0];
for(int i = 1;i< arr.length;i++){
if(max < arr[i]){
max = arr[i];
}
}
//Iterating over elements array
for (int i = 0; i < arr.length; i++) {
if(arr[i] == max){
continue;
}
//Iterating over primes array
for (int j = 0; j < primes.length; j++) {
int num = arr[i];
boolean cont = false;
for (int k = 1; k < 10; k++) {
num *= primes[j];
if(num == max){
cont = true;
break;
}else if(num < max){
continue;
}else{
if(j == primes.length -1){
return false;
}
break;
}
}
if(cont){
break;
}
}
}
return true;
}
}
```

Step1. Find LCM of all numbers in the array O(n)

Step2. For each number a[i]

- Divide LCM by a[i]

- Use each input prime number to divide the result to remove all factors of input prime numbers (can use modulo to check divisibility)

- If left over number is not 1, return false;

Step 3: Return true

First make a sieve of 10^5 according to constraints given.Also make a hash of given primes.Now take a variable, gcd_val and then find gcd of all elements. Now divide the array elements by this gcd_val. Now find each elements factorization, and check whether each elements factor is available in the hash or not. If any element's factor of given array is not in the hash,then print as "not possible", else print "possible".

- pawan kumar July 31, 2016Time complexity - O(nlogn)

Space complexity - O(n+k)

as per constraints - 1<=arr[i],n,k<=10^5

Example:1 -arr[]={ 7,14,21 }

primes[]={2,3}

gcd_val = 7

after dividing by gcd_val = 7, arr[]={1,2,3}

Example:2 arr[]={2,4,6}

primes[]={3}

gcd_val = 2

after dividing by gcd_val=2, arr[]={1,2,3}

here 2 is not in our hash, so "not possible".

** If anyone find anything wrong with my approach, feel free to correct me...:) **