Comparing Array Multiplicities in Java


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

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:

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:

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.

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:

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:

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:

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

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)); 
  }
}