Evaluating Array Multiplicities in Java


The problem

Given two arrays a and b write a operate comp(a, b) (orcompSame(a, b)) that checks whether or not the 2 arrays have the “similar” parts, with the identical multiplicities. “Similar” means, right here, that the weather in b are the weather in a squared, whatever the order.

Examples

Legitimate arrays

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

comp(a, b) returns true as a result of in b 121 is the sq. of 11, 14641 is the sq. of 121, 20736 the sq. of 144, 361 the sq. of 19, 25921 the sq. of 161, and so forth. It will get apparent if we write b‘s parts when it comes to 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 instance, we modify the primary quantity to one thing else, comp could 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 as a result of in b 132 isn’t the sq. of any variety 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 as a result of in b 36100 isn’t the sq. of any variety of a.

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

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

The answer in Java code

Possibility 1:

import java.util.*;
import java.io.*;
import java.util.Arrays;

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

Possibility 2:

import java.util.Arrays;

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

Possibility 3:

public class AreSame {
    public static boolean comp(int[] a, int[] b) {
        if (a == null || b == null || a.size != b.size) return false;

        int sumA = 0;
        int sumB = 0;

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

        return sumA == sumB;
    }
}

Take a look at circumstances to validate our answer

import static org.junit.Assert.*;
import org.junit.Take a look at;

public class AreSameTest {

  @Take a look at
  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)); 
  }
  @Take a look at
  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)); 
  }
  @Take a look at
  public void test3() {
    int[] a = new int[]{};
    int[] b = new int[]{1};
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Take a look at
  public void test4() {
    int[] a = new int[]{};
    int[] b = new int[]{};
    assertEquals(true, AreSame.comp(a, b)); 
  }
  @Take a look at
  public void test5() {
    int[] a = new int[]{};
    int[] b = null;
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Take a look at
  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)); 
  }
  @Take a look at
  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)); 
  }
  @Take a look at
  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)); 
  }
  @Take a look at
  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)); 
  }
  @Take a look at
  public void test10() {
    int[] a = new int[]{2, 2, 3};
    int[] b = {4, 9, 9};
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Take a look at
  public void test1a() {
    int[] a = new int[]{2, 2, 3};
    int[] b = {4, 4, 9};
    assertEquals(true, AreSame.comp(a, b)); 
  }
  @Take a look at
  public void test2a() {
    int[] a = new int[]{4, 4};
    int[] b = {1, 31};
    assertEquals(false, AreSame.comp(a, b)); 
  }
  @Take a look at
  public void test3a() {
    int[] a = new int[]{3, 4};
    int[] b = {0, 25};
    assertEquals(false, AreSame.comp(a, b)); 
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *