TopcoderArchiveVisit Topcoder
SRM5 Nov 2009
SRM 452

EggCartons

Problem statement, definition, constraints, and public examples.

Problem Statement

There are two types of egg cartons. One type contains 6 eggs and the other type contains 8 eggs. John wants to buy exactly n eggs. Return the minimal number of egg cartons he must buy. If it's impossible to buy exactly n eggs, return -1.

Definition

Class:
EggCartons
Method:
minCartons
Parameters:
int
Returns:
int
Method signature:
int minCartons(int n)
(be sure your method is public)

Constraints

  • n will be between 1 and 100, inclusive.

Examples

  1. 20
    Returns: 3
    He should buy 2 cartons containing 6 eggs and 1 carton containing 8 eggs. In total, he buys 3 egg cartons.
  2. 24
    Returns: 3
    There are two ways to buy 24 eggs: buy 4 cartons containing 6 eggs or buy 3 cartons containing 8 eggs. Minimize the number of cartons.
  3. 15
    Returns: -1
    He can't buy an odd number of eggs.
  4. 4
    Returns: -1
Back to all problems