An array a is called beautiful if for every pair of numbers a_i, a_j where (i \ne j), there exists an a_k such that a_k = a_i \times a_j. Note that k can be equal to i or j too.
Find out whether the given array a is beautiful or not!
Since you are trying to solve this problem Naively, you are trying to check for every possible pair, whether there exists any a_k as mentioned in the problem statement.
Note that there are exactly N\times (N - 1) pairs of indices (i, j),\ i\ne j. You are checking for all such possible pairs, using the below two for loops.
But we are cheating here. We aren’t checking for all pairs of (i, j) where i\ne j; instead, we are checking for all pairs of (i, j) where i\lt j. Hence the number of pairs we are checking reduces a bit.
Using the condition i \lt j, the number of pairs reduces to \cfrac{N\times (N - 1)}{2}
Therefore, we have to check if(count == (N * (N - 1)) / 2) and accordingly print the required output.
Note: The time complexity of your code is O(N^3). We can reduce it to O(N^2) using hashing and O(N\times \sqrt{A_{max}}) using factorisation. There can be a better approach of time complexity O(N\log{A_{max}}) but I cannot think of it as of now.
PS: The value \cfrac{N\times (N-1)}{2} can exceed the limits of Integer.
Edit: The problem is Ad-hoc . It can be solved in O(N), with a bit of case work.