TopcoderArchive
Archive
Algorithm problem

BulkSales

Problem statement, definition, constraints, and public examples.

Problem Statement

You have lengths of fabric available to sell, given in int[] lengths. When a customer wants to purchase some, they can purchase any quantity that can be made from either a single piece, or multiple pieces together. Pieces cannot be subdivided. Determine the total number of unique quantities that can be purchased.

Definition

Class:
BulkSales
Method:
numPossible
Parameters:
int[]
Returns:
int
Method signature:
int numPossible(int[] lengths)
(be sure your method is public)

Constraints

  • lengths will contain between 1 and 50 elements, inclusive.
  • Each element of lengths will be between 1 and 1000, inclusive.

Examples

  1. { 1, 2, 3 }
    Returns: 6
    We could sell a length of 1, 2, or 3 easily. Selling two pieces together, we can sell a total length of 4 or 5. Selling all three pieces gets us a length 6. So, there are six total quantities we can sell.
  2. { 1, 1, 10 }
    Returns: 5
    Here we could sell 1, 2, 10, 11, or 12... so five different lengths. (Note that we can have multiple pieces of the same length available.)
  3. { 1, 1, 1, 1, 1 }
    Returns: 5
    We have several pieces, but the possible totals are only 1 through 5.
Back to all problems