@Test public void randomTest3sum() { int N = 5; for (int i = 0; i < 700; i++) { int m = i % 100; if (m == 99) N <<= 1; int[] input = Shuffler.getRandomArray(N, -m * m, m * m); assertEquals(Arrays.toString(input), ThreeSum.count(input) != 0, Reduction.threeSum(input)); } }
public static boolean threeSum(int[] A) { int max = 0; for (int i : A) { max = Math.max(max, Math.abs(i)); } max++; int[] newA = new int[A.length + 1]; for (int i = 0; i < A.length; i++) newA[i] = max + A[i]; newA[A.length] = -3 * max; return FourSum.solveInN2(newA); }
Reduction { public static boolean threeSum(int[] A) { int max = 0; for (int i : A) { max = Math.max(max, Math.abs(i)); } max++; int[] newA = new int[A.length + 1]; for (int i = 0; i < A.length; i++) newA[i] = max + A[i]; newA[A.length] = -3 * max; return FourSum.solveInN2(newA); } }
Reduction { public static boolean threeSum(int[] A) { int max = 0; for (int i : A) { max = Math.max(max, Math.abs(i)); } max++; int[] newA = new int[A.length + 1]; for (int i = 0; i < A.length; i++) newA[i] = max + A[i]; newA[A.length] = -3 * max; return FourSum.solveInN2(newA); }  }
Reduction { public static boolean threeSum(int[] A) { int max = 0; for (int i : A) { max = Math.max(max, Math.abs(i)); } max++; int[] newA = new int[A.length + 1]; for (int i = 0; i < A.length; i++) newA[i] = max + A[i]; newA[A.length] = -3 * max; return FourSum.solveInN2(newA); }  static boolean threeSum(int[] A); static boolean threeLinear(int[] A); }
Reduction { public static boolean threeSum(int[] A) { int max = 0; for (int i : A) { max = Math.max(max, Math.abs(i)); } max++; int[] newA = new int[A.length + 1]; for (int i = 0; i < A.length; i++) newA[i] = max + A[i]; newA[A.length] = -3 * max; return FourSum.solveInN2(newA); }  static boolean threeSum(int[] A); static boolean threeLinear(int[] A);  }