I practice for an exam in Java. One of the questions I faced today is: Given an array with n numbers, I need to check if there are 2 subarrays(doesn't have to be equal) that their multiplication equals - if there are, will return true, else false. for example : if the array is : {2,15,3,4,2,5} - will return True if the array is : {2,4,6,2,3,4} - will return False.
the answer must be recursive, without any loops.
so I thought that if there are 2 sub arrays that their multiplication equal it means that the total multiplication of the whole array must be a square root number. for example at the first array, it's 3600 which is 60.
So far I couldn't find any case that it won't work for, but still not sure 100% that it will cover all the possible cases.
This is my code for that:
public static boolean splitEqualMult(int[] a) {
double multi = isArrSqrt(a,0);
if(Math.sqrt(multi) == Math.floor(Math.sqrt(multi))) {
return true;
}
return false;
}
private static double isArrSqrt(int[] a, int i) {
if(i == a.length) {
return 1;
}
return a[i] * isArrSqrt(a,i+1);
}
looking to hear your thoughts!