Best Maximum Product
All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
After learning to solve maximum sum subarray problem, it's time for you to solve the maximum product subsequence.
You're given an array of N integers, and a number K.
The maximum K-product of the array is the maximum product of any K length subsequence of the array. For example, the maximum 2-product of the array [-5, 3, 4, -6] is 30 because the product of the subsequence [-5, -6] is 30 and it is impossible to achieve larger subsequence product.
What is the maximum K-product you can achieve? Since the answer can be very large, output it modulo 109 + 7.
Note: Recall that the result of a modulo operation is always non-negative. For example, -8 mod 5 = 2.
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains two space separated integers N and K.
The second line contains N space-separated integers A1, A2, ..., AN, denoting the elements of the array.
For each test case, output a single line containing the maximum K-product of the array.
- 1 ≤ T ≤ 105
- 1 ≤ K ≤ N ≤ 105
- -109 ≤ Ai ≤ 109
- The sum of the Ns over all the test cases in a single test file is ≤ 5×105
Input: 4 4 2 5 -3 -4 6 4 2 -5 3 4 -6 3 2 10000 100000 100000 2 1 -1 -2 Output: 30 30 999999937 1000000006
Example case 1. The maximum 2-product of the array [5, -3, -4, 6] is 30 and is achieved by the subsequence [5, 6].
Example case 2. The maximum 2-product of the array [-5, 3, 4, -6] is 30 and is achieved by the subsequence [-5, -6].
Example case 3. The maximum 2-product of the array [10000, 100000, 100000] is 10000000000 and is achieved by the subsequence [100000,100000]. Modulo 109 + 7, this is 999999937.
Example case 4. The maximum 1-product of the array [-1, -2] is -1 and is achieved by the subsequence [-1]. Modulo 109 + 7, this is 1000000006.
The reason why -1 mod (109 + 7) = 1000000006 is that for positive m, a mod m is defined as the smallest nonnegative integer b such that a - b is divisible by m.
|Tags||greedy, kevinsogo, simple, snckpb16|
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, PYP3, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.