Problem10966--Codility - MaxProductOfThree

10966: Codility - MaxProductOfThree

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 512 MiB

Description

Maximize A[P] * A[Q] * A[R] for any triplet (P, Q, R).
A non-empty array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N).
For example, array A such that:
A[0] = -3 
A[1] = 1 
A[2] = 2 
A[3] = -2 
A[4] = 5 
A[5] = 6
contains the following example triplets:
  • (0, 1, 2), product is −3 * 1 * 2 = −6
  • (1, 2, 4), product is 1 * 2 * 5 = 10
  • (2, 4, 5), product is 2 * 5 * 6 = 60
Your goal is to find the maximal product of any triplet.

Input

Write an efficient algorithm for the following assumptions:
  • $N$ is an integer within the range $[3..100,000]$;
  • each element of array A is an integer within the range $[−1,000..1,000]$.

Output

find the maximal product of any triplet.

Sample 1 Input

6
-3 1 2 -2 5 6

Sample 1 Output

60

Sample 2 Input

6
-10 1 2 -20 5 6

Sample 2 Output

1200

HINT

Codility.

Source/Category