Problem Statement
The expression "sqrt(12) + sqrt(48)" can be simplified as follows:
sqrt(12) + sqrt(48) = sqrt(4*3) + sqrt(16*3) = 2*sqrt(3) + 4*sqrt(3) = 6*sqrt(3) = sqrt(36*3) = sqrt(108)
Given a list of integers, A, return a second list of integers, B, such that the sum of the square roots of the elements in B equals the sum of the square roots of the elements in A. B should contain as few elements as possible. The list with the fewest elements is guaranteed to be unique. The elements in your returned list B should be sorted from smallest to largest.
A will be given as a
For example, given the integers { 9, 16, 25 }, the sum of the square roots is 3 + 4 + 5, which is 12. The sum of the square roots of the list { 121, 1 } is also 12, but there is an even shorter list: { 144 }, which is the correct answer.
Definition
- Class:
- SumOfSquareRoots
- Method:
- shortestList
- Parameters:
- int[]
- Returns:
- int[]
- Method signature:
- int[] shortestList(int[] A)
- (be sure your method is public)
Constraints
- A will contain between 1 and 50 elements, inclusive.
- Each element of A will be between 1 and 1000, inclusive.
Examples
{12, 48}
Returns: { 108 }
This is the first example in the problem statement.
{9, 16, 25}
Returns: { 144 }
This is the second example in the problem statement.
{3, 4}
Returns: { 3, 4 }
{4, 3}
Returns: { 3, 4 }
The square root of 4 plus the square root of 3 is ~3.7320508. There is no way to express this as the square root of a single integer, so the correct answer is { 3, 4 }.
{1}
Returns: { 1 }
{1, 1}
Returns: { 4 }
{1, 1, 1}
Returns: { 9 }
{5, 3, 5}
Returns: { 3, 20 }
{1, 3, 5, 12, 20}
Returns: { 1, 27, 45 }
{1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }
Returns: { 961, 1922 }
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 }
Returns: { 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, 46, 47, 54, 63, 90, 99, 180, 300, 450, 784 }
{25, 16, 9}
Returns: { 144 }
{ 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 }
Returns: { 2500000 }
{ 313, 934, 939, 655, 11 }
Returns: { 11, 313, 655, 934, 939 }
{ 35, 924, 676, 866, 866 }
Returns: { 35, 676, 924, 3464 }
{ 72, 893, 288, 18, 838 }
Returns: { 838, 882, 893 }
{ 176, 338, 162, 11, 98 }
Returns: { 275, 1682 }
{ 752, 621, 139, 960, 15, 950, 49, 550, 887, 15 }
Returns: { 49, 139, 550, 621, 752, 887, 950, 1500 }
{ 1, 416, 158, 576, 256, 841, 121, 650, 891, 418 }
Returns: { 158, 418, 891, 2106, 6561 }
{ 167, 314, 838, 156, 199, 784, 16, 808, 18, 936, 715, 838, 2, 312, 668, 882, 172, 650, 649, 184 }
Returns: { 156, 172, 184, 199, 312, 314, 649, 715, 808, 1024, 1250, 1503, 3146, 3352 }
{ 604, 767, 106, 14, 75, 377, 347, 751, 12, 162, 386, 604, 350, 838, 877, 918, 37, 235, 925, 484, 947, 568, 928, 3, 588, 121, 749, 108, 9, 767 }
Returns: { 106, 162, 235, 347, 377, 386, 504, 568, 749, 751, 838, 877, 918, 928, 947, 1296, 1332, 2352, 2416, 3068 }
{ 417, 672, 540, 540, 288, 722, 529, 499, 514, 658, 285, 706, 560, 421, 920, 168, 636, 207, 887, 352, 766, 425, 550, 450, 560, 70, 692, 102, 63, 832, 102, 541, 289, 42, 766, 98, 832, 385, 339, 352 }
Returns: { 63, 70, 207, 285, 339, 385, 408, 417, 421, 425, 499, 514, 541, 636, 658, 692, 706, 887, 920, 1600, 2058, 2160, 2240, 3064, 3328, 3718, 5618 }
{ 453, 221, 195, 484, 183, 542, 643, 566, 662, 176, 70, 709, 284, 359, 855, 317, 845, 579, 853, 603, 279, 240, 278, 181, 771, 186, 707, 531, 154, 683, 611, 604, 593, 276, 277, 74, 851, 190, 267, 476, 358, 354, 52, 381, 103, 318, 188, 556, 864, 194 }
Returns: { 52, 70, 74, 103, 154, 176, 181, 183, 186, 188, 190, 194, 195, 221, 240, 267, 276, 277, 278, 279, 284, 317, 318, 354, 358, 359, 381, 453, 476, 484, 531, 542, 556, 566, 579, 593, 603, 604, 611, 643, 662, 683, 707, 709, 771, 845, 851, 853, 855, 864 }
{ 690, 801, 406, 955, 531, 410, 426, 192, 356, 167, 195, 356, 356, 784, 96, 617, 801, 99, 188, 118, 954, 384, 44, 655, 23, 848, 835, 386, 118, 652, 286, 77, 864, 870, 179, 661, 478, 108, 936, 759, 942, 894, 176, 451, 611, 223, 395, 846, 146, 373 }
Returns: { 23, 77, 146, 167, 179, 188, 195, 223, 286, 373, 386, 395, 406, 410, 426, 451, 472, 478, 531, 588, 611, 617, 652, 655, 661, 690, 759, 784, 835, 846, 848, 870, 891, 894, 936, 942, 954, 955, 3456, 12816 }
{ 910, 246, 988, 57, 583, 256, 830, 189, 674, 713, 553, 305, 567, 625, 433, 196, 451, 576, 947, 141, 150, 294, 36, 144, 577, 363, 958, 980, 235, 7, 516, 813, 516, 793, 847, 567, 36, 728, 959, 150, 3, 880, 277, 720, 63, 643, 501, 85, 951, 791 }
Returns: { 57, 85, 141, 189, 235, 246, 277, 305, 432, 433, 451, 501, 553, 577, 583, 643, 674, 713, 728, 791, 793, 813, 830, 880, 910, 947, 951, 958, 959, 988, 1734, 2064, 3380, 7623, 10609 }
{ 9, 96, 56, 95, 2, 73, 75, 9, 34, 74, 71, 51, 72, 4, 32, 64, 43, 11, 92, 10, 51, 64, 16, 71, 47, 98, 63, 54, 16, 4, 40, 63, 20, 99, 49, 85, 93, 71, 23, 18, 74, 43, 12, 43, 98, 63, 1, 56, 92, 34 }
Returns: { 20, 47, 73, 85, 90, 93, 95, 136, 147, 176, 204, 224, 294, 296, 387, 567, 575, 639, 1568, 1764 }
{ 1, 8, 8, 2, 8, 8, 2, 2, 8, 2, 1, 8, 10, 1, 7, 10, 9, 4, 6, 9, 7, 2, 2, 6, 9, 4, 6, 9, 4, 2, 9, 7, 7, 6, 2, 6, 9, 8, 10, 9, 4, 2, 8, 1, 6, 7, 8, 10, 7, 7 }
Returns: { 160, 216, 343, 1089, 1458 }
{ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }
Returns: { 961, 1922 }
{ 6 }
Returns: { 6 }
{ 999, 998, 997, 996, 995, 994, 993, 992, 991, 990, 999, 998, 997, 996, 995, 994, 993, 992, 991, 990, 999, 998, 997, 996, 995, 994, 993, 992, 991, 990, 999, 998, 997, 996, 995, 994, 993, 992, 991, 990, 999, 998, 997, 996, 995, 994, 993, 992, 991, 990 }
Returns: { 24750, 24775, 24800, 24825, 24850, 24875, 24900, 24925, 24950, 24975 }