@Test public void basicTest3sum() { assertTrue(Reduction.threeSum(new int[]{2, 1, -3})); assertFalse(Reduction.threeSum(new int[]{2, 1, -2, -2})); assertTrue(Reduction.threeSum(new int[]{-7, 2, 1, -3, 6})); assertFalse(Reduction.threeSum(new int[]{-9, 2, 11, -3, 5, 88})); }
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);  }