package algorithms;

public class MaxPairwiseProduct {
	static long getMaxPairwiseProduct(int[] numbers) {
		if (numbers.length == 1) {
			return 0;
		}
		Integer maxIndex1 = null;
		Integer maxIndex2 = null;
		for (int i = 0; i < numbers.length; ++i) {
			if (maxIndex1 == null || numbers[i] > numbers[maxIndex1]) {
				maxIndex2 = maxIndex1;
				maxIndex1 = i;
			} else if (maxIndex2 == null || numbers[i] > numbers[maxIndex2]) {
				maxIndex2 = i;
			}
		}
		return ((long) (numbers[maxIndex1])) * numbers[maxIndex2];
	}

	public static void main(String[] args) {
		System.out.println(
				"Type number to calculate the maximum pairwise product. "
						+ "The first number is a total count of numbers.");
		FastScanner scanner = new FastScanner(System.in);
		int numberOfNumbers = scanner.popNextInt();
		if (numberOfNumbers > Integer.MAX_VALUE) {
			throw new IllegalArgumentException("Too many numbers, the max is: " + Integer.MAX_VALUE);
		}
		int[] numbers = new int[numberOfNumbers];
		for (int i = 0; i < numberOfNumbers; i++) {
			numbers[i] = scanner.popNextInt();
		}
		System.out.println(getMaxPairwiseProduct(numbers));
	}
}
@@ -14,16 +14,42 @@
 ithms;%0A%0A
+import java.util.Arrays;%0A%0A
 public c
@@ -482,22 +482,29 @@
 %7D%0A%09%09%7D%0A%09%09
-return
+long result =
  ((long)
@@ -548,16 +548,877 @@
 ndex2%5D;%0A
+%09%09checkAgainstAlgorithmWithSorting(numbers, result);%0A%09%09return result;%0A%09%7D%0A%0A%09private static void checkAgainstAlgorithmWithSorting(int%5B%5D numbers, long result) %7B%0A%09%09long result2 = getMaxPairwiseProductWithSorting(numbers);%0A%09%09if (result != result2) %7B%0A%09%09%09throw new RuntimeException(%22getMaxPairwiseProduct returned %22 + result%0A%09%09%09%09%09+ %22 while getMaxPairwiseProductWithSorting returned %22 + result2);%0A%09%09%7D%0A%09%7D%0A%0A%09// This method is not as efficient (n logn) but it's used as a result verification experiment%0A%09private static long getMaxPairwiseProductWithSorting(int%5B%5D numbers) %7B%0A%09%09Integer%5B%5D newArray = new Integer%5Bnumbers.length%5D;%0A%09%09for (int i = 0; i %3C numbers.length; i++) %7B%0A%09%09%09newArray%5Bi%5D = numbers%5Bi%5D;%0A%09%09%7D%0A%09%09if (newArray.length == 1) %7B%0A%09%09%09return 0;%0A%09%09%7D%0A%09%09Arrays.sort(newArray, (x, y) -%3E x - y);%0A%09%09return (long) newArray%5BnewArray.length - 1%5D * newArray%5BnewArray.length - 2%5D;%0A
 %09%7D%0A%0A%09pub

Add a parallel verification mechanism