algorithm - Proof: Check if two integer arrays are permutations of each other using linear time and constant space -
i interested in creating simple array problem running time , space constraints. seems have found solution problem. please read initial description comment of problem in following java code:
/* * problem: given 2 integer arrays, , b, return whether array permutation of array b. * running time complexity: o(n) * space complexity: o(1) * example 1: * = [1, 2, -3] * b = [2, 3, 1] * false * example 2: * = [1, 2, 3] * b = [0, 1] * false * example 3: * = [2, 7, 3, 5] * b = [5, 7, 2, 3] * true * example 4: * = [1, -2, 10000] * b = [10000, 1, -2] * true * example 5: * = [1, 2, 2, 3] * b = [3, 2, 1, 3] * false * example 6: * = [2, 2, 4, 4] * b = [3, 3, 3, 3] * false * example 7: * = [4, 4, 2, 2, 4, 4] * b = [4, 3, 3, 3, 3, 4] * false * ---------------------- * input 2 space separated lines of integers * output true or false * terminal example: * 1 4 9 25 * 4 25 9 2 * false * ---------------------- * solution: * 1. average displacement (delta) between elements of array , array b equals 0 * , * 2. xor-ing of values between elements of array , array b equals 0 * , * 3. mins same , maxs same * @author (david brewster) * @version (27.01.2016) (requires java 1.8) */ import java.util.scanner; import java.util.arrays; public class arrayprob { public static int xorcomparison(int[] a, int[] b, int i, int xortotal) { return == a.length ? xortotal : xorcomparison(a, b, + 1, xortotal ^ a[i] ^ b[i]); } public static int deltacomparison(int[] a, int[] b, int i, int deltatotal) { return == a.length ? deltatotal : deltacomparison(a, b, + 1, deltatotal + a[i] - b[i]); } public static int mincomparison(int[] a, int[] b, int i, int amin, int bmin) { return == a.length ? amin-bmin : mincomparison(a, b, + 1, a[i] < amin ? a[i] : amin, b[i] < bmin ? b[i] : bmin); } public static int maxcomparison(int[] a, int[] b, int i, int amax, int bmax) { return == a.length ? amax-bmax : maxcomparison(a, b, i+1, a[i] > amax ? a[i] : amax, b[i] > bmax ? b[i] : bmax); } public static boolean arepermutations(int[] a, int[] b) { if (a.length == b.length) { boolean d = xorcomparison(a, b, 0, 0) == 0; boolean e = deltacomparison(a, b, 0, 0) == 0; boolean f = maxcomparison(a, b, 0, integer.min_value, integer.min_value) == 0; boolean g = mincomparison(a, b, 0, integer.max_value, integer.max_value) == 0; return d && e && f && g; } else { return false; } } public static void main(string[] args) { scanner input = new scanner(system.in); int[] = arrays.stream(input.nextline().split(" ")).maptoint(integer::parseint).toarray(); int[] b = arrays.stream(input.nextline().split(" ")).maptoint(integer::parseint).toarray(); system.out.println(arepermutations(a, b)); } }
even thought algorithm seems work cases (at least have tested far), how go proving solution correct 100% of time?
what trying computing fixed size fingerprint of each array (representing multiset) , determining equality of multisets comparing fingerprints.
this not possible because there infinite number of multisets if space constant, there limited number of fingerprints. inevitably find examples 2 different multisets map same fingerprint.
Comments
Post a Comment