The challenge

Given two arrays a and b write a function comp(a, b) (orcompSame(a, b)) that checks whether the two arrays have the “same” elements, with the same multiplicities. “Same” means, here, that the elements in b are the elements in a squared, regardless of the order.

Examples

Valid arrays

1
2
a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b‘s elements in terms of squares:

1
2
a = [121, 144, 19, 161, 19, 144, 19, 11] 
b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]

Invalid arrays

If, for example, we change the first number to something else, comp may not return true anymore:

1
2
a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 132 is not the square of any number of a.

1
2
a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 36100 is not the square of any number of a.

Remarks

  • a or b might be [] or {}.
  • a or b might be null.

If a or b are null, the problem doesn’t make sense so return false.

The solution in Java code

Option 1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import java.util.*;
import java.io.*;
import java.util.Arrays;

public class AreSame {
  public static boolean comp(int[] a, int[] b) {
    if ((a == null) || (b == null)) return false;
    int[] aa = Arrays.stream(a).map(n -> n * n).toArray();
    Arrays.sort(aa);
    Arrays.sort(b);
    return (Arrays.equals(aa, b));
  }
}

Option 2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import java.util.Arrays;

public class AreSame {
  public static boolean comp(final int[] a, final int[] b) {
    return a != null && b != null && a.length == b.length && 
        Arrays.equals(
            Arrays.stream(a).map(i -> i * i).sorted().toArray(), 
            Arrays.stream(b).sorted().toArray()
        );
  }
}

Option 3:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class AreSame {
    public static boolean comp(int[] a, int[] b) {
        if (a == null || b == null || a.length != b.length) return false;

        int sumA = 0;
        int sumB = 0;

        for (int i = 0; i < a.length; i++) {
            sumA += Math.abs(a[i]);
            sumB += Math.sqrt(b[i]);
        }

        return sumA == sumB;
    }
}

Test cases to validate our solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import static org.junit.Assert.*;
import org.junit.Test;

public class AreSameTest {

  @Test
  public void test1() {
    int[] a = new int[]{121, 144, 19, 161, 19, 144, 19, 11};
    int[] b = new int[]{121, 14641, 20736, 361, 25921, 361, 20736, 361};
    assertEquals(true, AreSame.comp(a, b)); 
  }
  @Test
  public void test2() {
    int[] a = new int[]{121, 144, 19, 161, 19, 144, 19, 11};
    int[] b = new int[]{11 * 11, 121 * 121, 144 * 144, 190 * 190, 161 * 161, 19 * 19, 144 * 144, 19 * 19};
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Test
  public void test3() {
    int[] a = new int[]{};
    int[] b = new int[]{1};
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Test
  public void test4() {
    int[] a = new int[]{};
    int[] b = new int[]{};
    assertEquals(true, AreSame.comp(a, b)); 
  }
  @Test
  public void test5() {
    int[] a = new int[]{};
    int[] b = null;
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Test
  public void test6() {
    int[] a = new int[]{121, 144, 19, 161, 19, 144, 19, 11, 1008};
    int[] b = {11 * 11, 121 * 121, 144 * 144, 190 * 190, 161 * 161, 19 * 19, 144 * 144, 19 * 19};
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Test
  public void test7() {
    int[] a = new int[]{121, 1440, 191, 161, 19, 144, 195, 11};
    int[] b = {11 * 11, 121 * 121, 1440 * 1440, 191 * 191, 161 * 161, 19 * 19, 144 * 144, 195 * 195};
    assertEquals(true, AreSame.comp(a, b)); 
  }
  @Test
  public void test8() {
    int[] a = new int[]{0, -14, 191, 161, 19, 144, 195, 1};
    int[] b = {1, 0, 14 * 14, 191 * 191, 161 * 161, 19 * 19, 144 * 144, 195 * 195};
    assertEquals(true, AreSame.comp(a, b)); 
  }
  @Test
  public void test9() {
    int[] a = new int[]{0, -14, 191, 161, 19, 144, 195, 1, 2};
    int[] b = {1, 0, 14 * 14, 191 * 191, 161 * 161, 19 * 19, 144 * 144, 195 * 195, 3};
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Test
  public void test10() {
    int[] a = new int[]{2, 2, 3};
    int[] b = {4, 9, 9};
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Test
  public void test1a() {
    int[] a = new int[]{2, 2, 3};
    int[] b = {4, 4, 9};
    assertEquals(true, AreSame.comp(a, b)); 
  }
  @Test
  public void test2a() {
    int[] a = new int[]{4, 4};
    int[] b = {1, 31};
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Test
  public void test3a() {
    int[] a = new int[]{3, 4};
    int[] b = {0, 25};
    assertEquals(false, AreSame.comp(a, b)); 
  }
}