5

We are given an array of size $N$ with integer entries $> 0$. We have to count the number of all such pairs $(a,b)$ with $a \leq b$ such that $a*b$ is divisible by $a + b$.

The obvious naive way is to check every pair and it takes $\mathcal{O}(N^2)$ time. This seems to be more of a maths problem rather than algorithmic.

Is it possible to improve the time complexity of this problem?

I have this idea.

Let $d = gcd(a,b)$. For some $x,y \in \mathbb{N}$ we have $a = dx$ and $b = dy$ with $gcd(x,y) = 1$. Since $gcd(x,x + y) = 1$ and $gcd(y,x + y) = 1$, we have

$(a+b) \lvert ab \Rightarrow d(x+y) \lvert d^2xy \Rightarrow (x+y) \lvert dxy \Rightarrow (x+y) \lvert d$

Let $d = k(x + y)$ for some $k \in \mathbb{N}$. Then $a = kx(x+y)$ and $b=ky(x+y)$ and the problem reduces to finding all such triplets $(k,x,y)$ with $x < y$ and $gcd(x,y) = 1$.

chelsea
  • 203
  • 1
  • 5

0 Answers0