@Test public void overflowTest() { boolean test = TwoSum.solve(new int[]{Integer.MAX_VALUE, Integer.MAX_VALUE}, -2); assertEquals(false, test); assertEquals(true, TwoSum.solve(new int[]{-1, Integer.MIN_VALUE, Integer.MAX_VALUE}, -1)); }
public static boolean solve(int[] arr, int tar) { LSDRadixSorter sorter = new LSDRadixSorter(); sorter.sort(arr); int i = 0, j = arr.length - 1; while (i <= j) { long cur = (long) arr[i] + arr[j]; if (cur > tar) j--; else if (cur < tar) i++; else return true; } return false; }
TwoSum { public static boolean solve(int[] arr, int tar) { LSDRadixSorter sorter = new LSDRadixSorter(); sorter.sort(arr); int i = 0, j = arr.length - 1; while (i <= j) { long cur = (long) arr[i] + arr[j]; if (cur > tar) j--; else if (cur < tar) i++; else return true; } return false; } }
TwoSum { public static boolean solve(int[] arr, int tar) { LSDRadixSorter sorter = new LSDRadixSorter(); sorter.sort(arr); int i = 0, j = arr.length - 1; while (i <= j) { long cur = (long) arr[i] + arr[j]; if (cur > tar) j--; else if (cur < tar) i++; else return true; } return false; }  }
TwoSum { public static boolean solve(int[] arr, int tar) { LSDRadixSorter sorter = new LSDRadixSorter(); sorter.sort(arr); int i = 0, j = arr.length - 1; while (i <= j) { long cur = (long) arr[i] + arr[j]; if (cur > tar) j--; else if (cur < tar) i++; else return true; } return false; }  static boolean solve(int[] arr, int tar); }
TwoSum { public static boolean solve(int[] arr, int tar) { LSDRadixSorter sorter = new LSDRadixSorter(); sorter.sort(arr); int i = 0, j = arr.length - 1; while (i <= j) { long cur = (long) arr[i] + arr[j]; if (cur > tar) j--; else if (cur < tar) i++; else return true; } return false; }  static boolean solve(int[] arr, int tar);  }