Statistics

Problem Statement for "BiggestRectangleHard"

Problem Statement

Little Josh has found several sticks of various lengths. He wants to form a rectangle with the biggest possible area, using these sticks as the perimeter. He is allowed to glue sticks together, but he is not allowed to break single sticks into multiple shorter sticks. Sticks may only be glued together at their endpoints, so a stick of length 2 and a stick of length 3 can only be glued together to form a stick of length 5.

For example, if Josh has sticks with lengths {1, 3, 3, 4, 5, 7}, he can create a 3 x 5 rectangle using the two sticks of length 3, the stick of length 5, and the sticks of lengths 1 and 4 glued together. This rectangle has an area of 15 square inches, which is the biggest area that can be achieved with these sticks.

You will be given a int[] lengths containing the lengths of the sticks in inches. Return the maximal area (in square inches) of a rectangle that can be created using these sticks. If it is impossible to form a rectangle, return -1.

Definition

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

Constraints

  • lengths will contain between 4 and 16 elements, inclusive.
  • Each element of lengths will be between 1 and 10, inclusive.

Examples

  1. {1, 3, 3, 4, 5, 7}

    Returns: 15

    The example from the problem statement.

  2. {9, 9, 5, 6, 2, 10}

    Returns: -1

    It is impossible to create a rectangle using these sticks.

  3. {3, 4, 7, 8, 10, 2, 9}

    Returns: 70

    The best rectangle is 7x10.

  4. {9, 2, 7, 9, 4, 9, 7, 10, 3}

    Returns: 224

    The best rectangle is 14x16.

  5. {9, 9, 10, 7, 7, 8, 7, 5, 8, 6, 9, 7, 7, 10, 9, 6}

    Returns: 961

    The best rectangle is a square 31x31.

  6. {2,6,9,4,7}

    Returns: -1

  7. {6,1,5,10,3,9,8,5}

    Returns: 120

  8. {6,2,10,2,9,1,5,5,4,9,10}

    Returns: 240

  9. {8,8,10,5,5,10,1,7,2,2,7,3}

    Returns: 289

  10. {3,1,3,2,7,8,9,4,5,2,1,2,1,5}

    Returns: 169

  11. {8,3,7,7,7,2,2,10,8,2,2,4,3,1}

    Returns: 272

  12. {7,6,9,6,7,9}

    Returns: 117

  13. {2, 6, 4, 10, 2, 8, 1, 8, 2, 1, 4, 8, 10}

    Returns: 272

  14. {10,2,3,8}

    Returns: -1

  15. {10,3,9,2,9,1,2,6,5,9,5,10,10,8,5,7}

    Returns: 625

  16. {8,1,7,6,7,7,9,10,9,5,9}

    Returns: 380

  17. {1,9,8,3,7,1,8,3,4,4,10,5,9,9}

    Returns: 400

  18. {6,5,4,6,8}

    Returns: -1

  19. {9,8,8,5,6,2,8,5,5,10,6}

    Returns: 323

  20. {3,9,3,3,3,10,1}

    Returns: 60

  21. {8,10,5,4,2,3,5,7,3,1,3,7,1}

    Returns: 210

  22. {7,4,9,6,1}

    Returns: -1

  23. {1,1,8,5,2,3,7,5,6,3,6,6,6,1,1,6}

    Returns: 272

  24. {9,4,7,4,3}

    Returns: -1

  25. {4,10,2,4,8,6,10,4,8}

    Returns: 196

  26. {5,6,8,5,10,10,8,9,9,9,6,7,7,10,5,7}

    Returns: 841

  27. {7,5,8,6,8,6,8,6,9,6,6,7,10,10,7,9}

    Returns: 870

  28. {6,9,7,6,7,10,8,7,6,7,8,6,7,7,10,8}

    Returns: 784

  29. {5,5,7,8,10,6,9,9,8,10,7,8,8,8,7,8}

    Returns: 870

  30. {7,6,9,10,8,10,10,6,8,6,6,9,5,10,8,10}

    Returns: 1024

  31. {9,8,9,9,9,8,9,9,7,6,9,10,9,9,7,5}

    Returns: 1089

  32. {6,7,10,7,8,6,7,7,10,5,8,10,6,8,10,9}

    Returns: 961

  33. {6,7,5,10,7,10,10,9,6,5,5,7,9,8,7,7}

    Returns: 870

  34. {5,8,6,8,9,8,6,7,8,8,7,6,8,6,6,10}

    Returns: 841

  35. {9,9,10,6,10,9,6,9,6,7,7,8,7,8,10,10}

    Returns: 960

  36. {5,10,8,6,9,7,8,8,6,7,7,7,10,8,5,7}

    Returns: 870

  37. {5,10,9,7,9,8,9,5,5,5,6,5,6,8,5,8}

    Returns: 756

  38. {6,10,6,5,10,5,10,5,10,8,6,6,6,5,6,9}

    Returns: 729

  39. {10,9,10,9,10,10,9,10,6,10,5,10,6,7,10,6}

    Returns: 1080

  40. {9,6,6,9,9,7,8,8,10,8,8,8,7,10,10,5}

    Returns: 1024

  41. {6,6,6,7,6,9,5,10,7,5,8,8,10,8,5,8}

    Returns: 812

  42. {8,7,6,10,7,10,10,8,7,5,6,7,6,8,6,9}

    Returns: 900

  43. {10,6,5,6,5,9,8,10,8,8,6,9,6,9,10,10}

    Returns: 900

  44. {5,10,6,8,10,6,9,10,9,5,10,5,7,7,9,10}

    Returns: 992

  45. {6,7,8,7,10,5,8,8,7,5,5,8,5,6,8,7}

    Returns: 756

  46. {8,10,8,8,10,10,8,8,9,7,7,7,7,7,7}

    Returns: 810

  47. {7,7,8,9,7,7,10,8,8,9,8,8,7,10}

    Returns: 700

  48. {10,7,7,9,8,10,10,9,10,9,10,9,10,8}

    Returns: 990

  49. {8,8,8,10,8,7,8,9,8,8,9,10,8,9}

    Returns: 864

  50. {9,7,8,9,7,10,9,8,10,7}

    Returns: 437

  51. {9,9,7,9,7,8,8,8,7,7,10,9}

    Returns: 600

  52. {9,8,10,9,10,7,8,10,9,10,10,10,9,7,8,9}

    Returns: 1120

  53. {10,7,7,10,8,7,9,9,7,7}

    Returns: 272

  54. {7,7,9,8,8,8,8,8,8,9}

    Returns: 391

  55. {10,7,9,10,7,7,9,10,10,8,8,7}

    Returns: 650

  56. {7,8,7,7,8,9,9,8,10,8,8,9,7,7}

    Returns: 780

  57. {7,7,10,9,9,8,9,10,9,7,7,10,10}

    Returns: 784

  58. {8,9,8,9,10,10,7,7,10,10,9}

    Returns: 500

  59. {7,7,7,9,9,8,9,8,10,10,7,9,7,8}

    Returns: 728

  60. {7,7,10,8,7,7,10,10,8,7}

    Returns: 255

  61. {7,9,10,10,9,9,10,8,7,10,10,7,9,7}

    Returns: 928

  62. {7,8,8,10,10,9,10,8,9,9,10,9,7}

    Returns: 810

  63. {10,8,7,9,10,9,9,7,9,7,9,9,8,9}

    Returns: 896

  64. {8,10,8,9,7,10,9,10,9,8,7}

    Returns: 475

  65. {10,7,7,10,9,10,8,8,10,10,7,7,8,9,8,7}

    Returns: 1020

  66. {9, 8, 10, 9, 10, 9, 8, 10, 9, 10, 10, 10, 9, 9, 8, 9}

    Returns: 1050

  67. {9, 9, 10, 9, 10, 9, 8, 10, 9, 10, 10, 10, 9, 9, 8, 9}

    Returns: 1369

  68. {9,8,10,8,8,10,8,8,9,8,10,9,9,8,10,8}

    Returns: 1225

  69. {8,10,10,9,8,8,9,8,9,8,10,9,10,10,9,8}

    Returns: 1080

  70. {10,9,8,9,9,10,10,8,8,9,10,8,8,8,8,8}

    Returns: 1225

  71. {10,10,9,9,9,9,9,9,9,9,10,9,10,9,10,10}

    Returns: 1406

  72. {9,9,9,9,9,10,10,9,9,9,10,10,9,10,9,9}

    Returns: 1044

  73. {10,10,9,9,9,9,9,10,9,9,9,10,10,10,9,9}

    Returns: 1406

  74. {10,10,10,10,9,10,9,10,10,9,10,9,9,9,6,2}

    Returns: 1218

  75. {9,10,9,10,9,9,9,10,9,9,9,9,9,9,5,5}

    Returns: 1044

  76. {10,9,9,9,9,10,9,10,10,9,10,10,9,10,3,3}

    Returns: 1140

  77. {10,10,9,10,10,9,10,9,10,10,9,10,9,10,6,2}

    Returns: 1110

  78. {9,9,10,9,9,9,9,10,9,9,9,9,9,10,10,10}

    Returns: 1044

  79. {10,10,9,10,10,10,10,10,9,9,10,10,10,9,8,2}

    Returns: 1170

  80. {10,10,10,10,10,9,10,9,10,10,10,9,9,10,8,9}

    Returns: 1140

  81. {10,10,9,9,9,9,10,10,9,10,10,10,10,9,8,9}

    Returns: 1110

  82. {10,10,9,10,10,10,9,10,9,9,10,10,9,10,6,5}

    Returns: 1330

  83. {10,9,10,10,9,9,9,10,9,10,10,10,10,9,7,4}

    Returns: 1160

  84. {10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10}

    Returns: 1600

  85. {10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,9}

    Returns: 1200

  86. {10,10,10,10,10,10,10,10,10,10,10,10,10,7,8,9}

    Returns: 1480

  87. {10,10,10,10,10,10,10,10,10,10,10,10,4,7,8,9}

    Returns: 1360

  88. {10,10,10,10,10,10,10,10,10,10,10,10,7,7,8,9}

    Returns: 1110

  89. {10,10,10,10,10,10,9,9,9,9,9,9,9,3,8,8}

    Returns: 1204

  90. {10,10,10,10,10,10,10,10,10,10,10,10,10,6,8,9}

    Returns: 900

  91. {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 8, 7, 10 }

    Returns: 1170

  92. {2, 6, 4, 10, 2, 8, 1, 8, 2, 1, 4, 8, 10 }

    Returns: 272

  93. {1, 3, 3, 4, 5, 7 }

    Returns: 15

  94. {1, 9, 9, 9, 9, 10, 10, 10, 10, 9, 1, 1, 1, 1, 1 }

    Returns: 420

  95. {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }

    Returns: 1600


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: