简体   繁体   中英

How to check if an array has squared elements of another array (regardless of order)?

a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361] returns true, coz b[] has squared values of elements in a[], regardless of order.

Here's what I tried...

static boolean comp(int a[], int b[]) {
        int x = 0;
        if (a.equals(b)) {
            for (int i : a) {
                for (int j : b) {
                    if (a[i] == b[j] * b[j]) {
                        x++;
                    } else {
                        x--;
                    }
                }
            }
        } else {
            return false;
        }
        if (x == a.length) {
            return true;
        } else {
            return false;
        }
    }

Simply this would work:

static boolean comp(int a[], int b[]) {

    for (int i : a) {
        if (!Arrays.asList(b).contains(i*i)) {
            return false;
        }
    }
    return true;
}

In short, you go through every value of a and b to see if the value of a squared is b. When you find a non-match, you automatically return false. Otherwise, true is returned.

public static Boolean compare(int[] a, int[] b) { Arrays.sort(a); Arrays.sort(b); for (int i = 0; i < a.length; i++) { if (a[i] * a[i] != b[i]) { return false; } } return true; }
static boolean comp(int a[], int b[]) {

    for (int i : a) {
        if (!Arrays.asList(b).contains(i*i)) {
            return false;
        }
    }
    return true;
}

Try using sets.

      int[] a = { 121, 144, 19, 161, 19, 144, 19, 11
      };
      int[] b = { 121, 14641, 20736, 361, 25921, 361, 20736, 361
      };
      Set<Integer> seta = Arrays.stream(a).map(r -> r * r).boxed().collect(
            Collectors.toSet());
      Set<Integer> setb =
            Arrays.stream(b).boxed().collect(Collectors.toSet());
      System.out.println(setb.containsAll(seta));

Note that this does not take into consideration duplicates.

List has a containsAll , I would use that; with Java 8+ and streams that might look like

static boolean comp(int a[], int b[]) {
    return Arrays.stream(b).distinct().boxed().collect(Collectors.toList())
            .containsAll(Arrays.stream(a).map(x -> x * x).boxed()
            .collect(Collectors.toList()));
}

Try sorting both arrays and iterating over both of them at the same, first squaring the number from the first array and then checking if it is equal to the element from the second array. Once you reach non-equal elements return false, otherwise return true. This should give you 2*n*log(n) + n if using quicksort.

public static Boolean compare(int[] a, int[] b) {
  Arrays.sort(a);
  Arrays.sort(b);
  for (int i = 0; i < a.length; i++) {
    if (a[i] * a[i] != b[i]) {
      return false;
    }
  }
  return true;
}
bool Same(int[] arr1, int[] arr2)
{
    for (int i = 0; i < arr1.Length; i++)
    {
        var correctIndex = Array.IndexOf(arr2, (int)Math.Pow(arr1[i], 2));
        if (correctIndex == -1)
        {
            return false;
        }
        arr2.Take(correctIndex);
    }
    return true;
}

in c#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM