Finding all possible pairs of square numbers in an array I am writing a program that allows me to find all possible pairs of square numbers including duplicates. We can also assume the array elements to be of positive integers only.
e.g an array of {5,25,3,25,4,2,25} will return [5,25],[5,25],[2,4],[5,25]
since 25 is square of 5.

Currently, I am using a nested for loop to find the squares. I'm just wondering if there is a better way to do this?

import java.lang.Math.*;

public static void main(String args[])
{
    int arr[] = {5,25,3,25,4,2,25};
    String s = "";

    for(int i =0; i &lt; arr.length;i++)
    {

        for(int j = 0;j &lt; arr.length;j++)
        {
            if(Math.sqrt(arr[i]) == arr[j])
            {
                s += arr[j] + "," + arr[i] + " ";
            }
        }

    }

    System.out.println(s);

}
Avoid string addition

String addition is not good for building up strings from many pieces inside of loops.  You should use StringBuilder instead.

StringBuilder sb = new StringBuilder();

// ... omitted ...

        sb.append(arr[j]).append(',').append(arr[j]).append(' ');

// ... omitted ...

String s = sb.toString();
System.out.println(s);


Avoid square-roots

Checking \$\sqrt x == y\$ is ... dangerous.


The results of the square-root are a float-point number, and may not exactly equal your integer value.
If you have a negative number in your list, Math.sqrt() will raise an exception, yet { -5, 25 } is a valid pair.


Testing x == y*y is safer, as long as there is no danger of y*y overflowing.

Avoid repeated calculations

for(int i =0; i &lt; arr.length;i++) {
    for(int j = 0;j &lt; arr.length;j++) {
        if(Math.sqrt(arr[i]) == arr[j]) {
            ...


In the inner-loop, i is constant.  Yet you are computing Math.sqrt(arr[i]) every time through the loop.  The value should not be changing, so you could compute it once, outside of the inner loop.

for(int i =0; i &lt; arr.length;i++) {
    double sqrt_arr_i = Math.sqrt(arr[i]);
    for(int j = 0;j &lt; arr.length;j++) {
        if(sqrt_arr_i == arr[j]) {
            ...


Pairs need distinct indices

If your input contains a single 0 or 1, it will mistakenly report that it has found a pair, since 0 == 0*0 and 1 == 1*1.  You can protect against this by adding i != j &amp;&amp; to your test.

If the input contains two 0's (or two 1's), your algorithm will emit 4 pairs: [first,first], [first,second], [second,first], and [second,second].  Adding the i != j guard will eliminate the first and last of those pairs, but it will still declare two pairs: [first,second], [second,first] since first² = second and first = second² would both be true.  You'd have to weight in on whether this would be two distinct pairs or not.

Formatting

Consistent and liberal use of white space is always recommended.  Add white space after every semicolon inside of for(...), on both sides of operators (i = 0 not i =0), and after every comma in {5,25,3,25,4,2,25}.



With the above recommendations, your function body would become:

int arr[] = { 5, 25, 3, 25, 4, 2, 25 };
StringBuilder sb = new StringBuilder();

for(int i = 0; i &lt; arr.length; i++)
{
    for(int j = 0; j &lt; arr.length; j++)
    {
        if(arr[i] == arr[j] * arr[j])
        {
            sb.append(arr[j]).append(',').append(arr[i]).append(" ");
        }
    }
}

String s = sb.toString();
System.out.println(s);




Additional considerations

You have a trailing space in your resulting string.  There are several tricks you can use to remove it.  However, an interesting alternative is to use StringJoiner:

StringJoiner sj = new StringJoiner(" ");

// ... omitted ...

        sj.add(arr[j] + "," + arr[j]);

// ... omitted ...

String s = sj.toString();
System.out.println(s);


When StringJoiner adds the second and successive strings, it automatically adds the delimiter specified in the constructor.