EggCartons
SRM 452 · 2009-11-05 · by rng_58
SRM 452 · 2009-11-05 · by rng_58 · Brute Force
Problem Statement
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.
Constraints
- n will be between 1 and 100, inclusive.
Examples
0)
20 Returns: 3
He should buy 2 cartons containing 6 eggs and 1 carton containing 8 eggs. In total, he buys 3 egg cartons.
1)
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.
2)
15 Returns: -1
He can't buy an odd number of eggs.
3)
4 Returns: -1
4)
1 Returns: -1
Submissions are judged against all 58 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class EggCartons with a public method int minCartons(int n) · 58 test cases · 2 s / 256 MB per case