## Adobe Interview Question for Backend Developers

• 0

Country: India
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
0
of 0 vote

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".
Time 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...:) **

Comment hidden because of low score. Click to expand.
0
of 0 vote

bool check(int a[])
{
int a = a;
int b = a;
int gcd = gcd(a, b);
int prime = 0;
if(!isPrime(gcd))
{
prime = Prime(gcd);
}
else
{
prime = gcd;
}

for(int i = 2 ; i < a.length - 1; i++){
if(a[i] % prime != 0){
return false;
}
}
return true;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

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;``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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;
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;
}

}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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

Comment hidden because of low score. Click to expand.
0
of 0 vote

test

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.