TopcoderArchive
Archive
Algorithm problem

TheBestProduct

Problem statement, definition, constraints, and public examples.

Problem Statement

Alice and Anna are playing a new game with some cards. In this game, Alice writes a number equal to -2,-1,0,1 or 2 in each of the cards. Anna must then pick exactly K cards. Anna's final score will be equal to the product of the numbers written in the K cards she picked.

You are given a int[] cards and int k. For each i, the i-th element of cards describes the number that was written in the card. Return the maximum score possible Anna can make in this game.

Definition

Class:
TheBestProduct
Method:
findIt
Parameters:
int[], int
Returns:
int
Method signature:
int findIt(int[] cards, int K)
(be sure your method is public)

Constraints

  • cards will contain between 1 and 30 elements, inclusive.
  • Each element of cards will be between -2 and 2, inclusive.
  • k will be between 1 and the number of elements in cards

Examples

  1. {-2, -1, 0, 1, 2}		
    1
    Returns: 2
    Anna must pick a single card. The best card is 2.
  2. {0,1,1,2,2,2,1}
    3
    Returns: 8
  3. {0,-1,-1,-2,-2,-2,-1}
    5
    Returns: 0
    One of the four cards should be 0, this way the final product is 0. Any combination of cards that doesn't include 0 will yield a negative result.
  4. {-1,-2,-2,-1,-2,1}
    5
    Returns: 8
    Anna should pick: -2, -2, -2, -1 and 1.
  5. {0,0,0,0,0,0,0,1}
    4
    Returns: 0
Back to all problems