package Codility;

import java.util.Arrays;

public class MaxProductOfThree {
	public static void main(String[] args) {
		int[] A = new int[] { -9, -6, 3, 2, 5, 7 };
		System.out.println(solution(A));
	}

	public static int solution(int[] A) {
		int[] array = Arrays.stream(A).distinct().sorted().toArray();
		int sum1 = 1;
		int sum2 = 1;

		for (int i = array.length - 3; i < array.length; i++) {
			sum1 = Math.multiplyExact(sum1, array[i]);
		}

		for (int i = 0; i < 2; i++) {
			sum2 = Math.multiplyExact(sum2, array[i]);
		}
		sum2 = Math.multiplyExact(sum2, array[array.length - 1]);

		return Math.max(sum1, sum2);

	}
}

@@ -38,16 +38,599 @@
 rrays;%0A%0A
+/**%0AA non-empty zero-indexed array A consisting of N integers is given.%0A%0AThe problem is to find the maximum product of 3 elements from given array.%0A%0AThe length of the array is between 3 and 100,000%0A%0Aeach element of array A is an integer within the range %5B%E2%88%921,000..1,000%5D%0A%0A    expected worst-case time complexity is O(N*log(N));%0A%0A    expected worst-case space complexity is O(1), %0Abeyond input storage (not counting the storage required for input arguments). Example:%0A%0A  a%5B0%5D = -3;%0A  a%5B1%5D = 7;%0A  a%5B2%5D = 2;%0A  a%5B3%5D = 1;%0A  a%5B4%5D = 5;%0A  a%5B5%5D = 7;%0Athe max product is a%5B1%5D*a%5B4%5D*a%5B5%5D = 245%0A*/%0A
 public c

Update MaxProductOfThree.java