title: Most efficient way to count number of pairs in an array whose product is a perfect square?
body: <p>My solution was this:</p>
  
  <p>To take all possible pairs (not the one which had same elements as the question demanded) , then check first element to be perfect square if true 
  check the other element to be perfect square.</p>
  
  <p>Checking for perfect square was simply getting square root and checking fractional part.</p>
  
  <p>I was asked this in an interview but it was required to be better.
  Can anyone give me a better approach.</p>
  
  <pre><code>int cnt = 0.0;
  for (int i = 0; i &lt; test; i++)  {
      for (int j = 0; j &lt; test; j++)  {
          if (!(i == j))  {
              double prod = Math.sqrt(arr[i]);
              int prodi = (int) prod;
              double frac = prod - prodi;
              if (frac == 0.0) {
                  //System.out.println("SAdfs");
                  prod=Math.sqrt(arr[j]);
                  prodi = (int)prod;
                  frac = prod-prodi;
                  if (frac == 0.0) {
                      cnt++;
                  }
              }
          }       
      }
  }
  
  double denom=test*(test-1);
  double result=cnt/denom;
  String strDouble = String.format("%.6f", result);
  System.out.println(result);
  </code></pre>