Problem Statement
You have a piece of paper that you need to fold to fit into a box with a given width and length. Each time you fold the paper, you can fold it in half across either its width or length, but you can only fold the paper 8 times (after 8 times, the paper is too dense to fold again).
You will be given a
Definition
- Class:
- PaperFold
- Method:
- numFolds
- Parameters:
- int[], int[]
- Returns:
- int
- Method signature:
- int numFolds(int[] paper, int[] box)
- (be sure your method is public)
Notes
- After the paper is folded, it is possible that it has fractional dimensions.
- The paper will fit into the box even if one or both of the dimensions are exactly the same as the corresponding box dimensions.
Constraints
- paper will contain exactly two elements.
- box will contain exactly two elements.
- Each element of paper and box will be between 1 and 10000, inclusive.
Examples
{8, 11}
{6, 10}
Returns: 1
By folding the paper so that the length is reduced from 11 to 5.5 inches, it will fit inside the box if you rotate it 90 degrees.
{11, 17}
{6, 4}
Returns: 4
There are two ways to fold the paper so that it fits within the box. First, you could fold the width in half to get 5.5 inches, and then fold the length in half three times to get 2.125 inches. Second, you could fold the width in half twice to get 2.25 inches, and then fold the length in half twice to get 4.25 inches. In this case, you must also rotate 90 degrees to fit the paper in.
{11, 17}
{5, 4}
Returns: 4
Now, you must use the second method to get it to fit: Fold the width and length each twice, and rotate 90 degrees. If you try fitting without rotating, it would take 5 folds.
{1000,1000}
{62,63}
Returns: -1
Folding in each direction 4 times, you can get the paper down to 62.5 x 62.5. However, this will not fit into the box because neither dimension fits in 62.
{512,2}
{2,2}
Returns: 8
{100,30}
{60,110}
Returns: 0
{1895, 6416}
{401, 1000}
Returns: 5
{3169, 3464}
{1785, 225}
Returns: 5
{5645, 3137}
{2229, 27}
Returns: -1
{8994, 5100}
{982, 6753}
Returns: 4
{7682, 6447}
{193, 436}
Returns: -1
{8453, 3489}
{5592, 6463}
Returns: 1
{9889, 9272}
{6568, 67}
Returns: -1
{3541, 9525}
{7555, 9968}
Returns: 0
{5022, 689}
{4937, 1855}
Returns: 1
{3664, 3995}
{87, 937}
Returns: 8
{9155, 8169}
{6135, 3463}
Returns: 3
{7983, 4143}
{9349, 1122}
Returns: 2
{1068, 9732}
{5416, 3972}
Returns: 1
{2056, 9360}
{1974, 5026}
Returns: 2
{8426, 9294}
{6857, 2810}
Returns: 3
{6403, 5814}
{3858, 37}
Returns: -1
{5755, 2026}
{4055, 3020}
Returns: 1
{9561, 5779}
{1501, 3556}
Returns: 4
{5108, 2924}
{5809, 9686}
Returns: 0
{3377, 7517}
{389, 3153}
Returns: 6
{2818, 4928}
{6509, 4757}
Returns: 0
{6343, 5242}
{5573, 3647}
Returns: 1
{7292, 9059}
{9255, 7465}
Returns: 0
{1574, 882}
{5123, 6123}
Returns: 0
{2349, 2412}
{7150, 8022}
Returns: 0
{7745, 9698}
{4288, 682}
Returns: 5
{4232, 7405}
{4519, 8760}
Returns: 0
{5421, 2805}
{3, 4863}
Returns: -1
{6533, 7604}
{1309, 48}
Returns: -1
{1482, 8640}
{2388, 6647}
Returns: 1
{1350, 2462}
{1440, 9103}
Returns: 0
{8304, 7859}
{2148, 6980}
Returns: 3
{9556, 9370}
{770, 514}
Returns: -1
{8830, 8903}
{7963, 48}
Returns: -1
{3724, 8080}
{46, 2032}
Returns: -1
{5918, 7087}
{2300, 85}
Returns: -1
{5006, 3607}
{1800, 8290}
Returns: 2
{5061, 1100}
{4675, 7479}
Returns: 0
{5540, 5216}
{1285, 3906}
Returns: 4
{978, 1187}
{490, 6135}
Returns: 1
{1998, 5645}
{515, 15}
Returns: -1
{7303, 8691}
{814, 276}
Returns: -1
{2528, 3911}
{4079, 639}
Returns: 2
{1242, 5184}
{2384, 4872}
Returns: 1
{6196, 9201}
{75, 1154}
Returns: -1
{1871, 2385}
{9656, 1088}
Returns: 1
{ 1895, 6416 }
{ 401, 1000 }
Returns: 5
{ 16, 16 }
{ 1, 1 }
Returns: 8
{ 256, 1 }
{ 1, 1 }
Returns: 8
{ 8, 11 }
{ 3, 2 }
Returns: 4
{ 11, 17 }
{ 6, 4 }
Returns: 4
{ 2, 1 }
{ 1, 1 }
Returns: 1
{ 1, 256 }
{ 1, 1 }
Returns: 8
{ 10000, 2 }
{ 10000, 4 }
Returns: 0
{ 11, 11 }
{ 5, 11 }
Returns: 2
{ 1, 257 }
{ 1, 1 }
Returns: -1
{ 7000, 7000 }
{ 2000, 2000 }
Returns: 4