Given two integer arrays like this:-
int[] a = { 2, 6, 10, 13, 17,18 };
int[] b = { 3, 7, 8, 9, 11, 15 };
How can I find pairs from these two arrays such that when multiplied they become perfect square?
For eg, in above arrays {2,8}
& {18,8}
are two pairs.
Right now my approach is brute-force, where I am looping through both arrays like this:-
int count = 0;
for (int i = 0; i < arr1.Length; i++)
{
for (int j = 0; j < arr2.Length; j++)
{
var x = arr1[i] * arr2[j];
long s = (long)Math.Sqrt(x);
if (x == s * s)
count += 1;
}
}
How can I do this efficiently?