PairwiseSums
SRM 182 · 2004-02-07 · by dgarthur
Problem Statement
Given a list of numbers, A = {a0, a1, ..., an-1}, its pairwise sums P are defined to be all numbers of the form ai + aj for 0 <= i < j < n. For example, if
- A = {1,2,3,4},
- P = {1+2, 1+3, 1+4, 2+3, 2+4, 3+4} = {3, 4, 5, 5, 6, 7}.
In general, it is very easy to compute the pairwise sums of a list, but it is much more difficult to perform this operation in reverse, extracting the original list given its pairwise sums. That is where you come in. Create a class PairwiseSums that contains a method reverse, which takes a
Constraints
- sums will contain between 1 and 50 elements inclusive.
- Each element of sums will contain between 1 and 50 characters inclusive.
- There will be a total of at least three integers in sums.
- Each element of sums will contain at least one integer.
- Each character in sums will either be a space (' '), or a digit ('0'-'9').
- Each integer in sums will be between 0 and 1000 inclusive, and will have no leading 0's.
- Each element of sums will have precisely one space between consecutive integers, and will have no leading or trailing spaces.
{"3 5 4 7 6 5"}
Returns: { 1, 2, 3, 4 }
This is the example from the problem statement.
{"0", "1000", "1000"}
Returns: { 0, 0, 1000 }
Note that A can contain 0 and duplicates, but it cannot contain negative integers or fractions.
{"15 30 45 60"}
Returns: { }
There exists no A whose collection of pairwise sums contains precisely four elements.
{"5 6 7 9 10 11"}
Returns: { }
A could be either {1,4,5,6} or {2,3,4,7}, so {} is returned.
{"910 689 882 565 922 815 457 674 495 653 755 948",
"631 988 881 523 740 561 719 727 410 767 660 302",
"519 340 498 603 960 853 495 712 533 691 643 536",
"178 395 216 374 893 535 752 573 731 428 645 466",
"624 287 108 266 325 483 304"}
Returns: { 35, 73, 143, 231, 252, 267, 393, 422, 460, 488, 500 }
{"5 7 8 8 6 4 6 8 5 6 7 5 7 8 6 8 6 4 4 5 6 6 5 5 6",
"4 4 7 5 4 8 8 4 7 4 8 6 6 7 4 8 8 5 5 5 8 8 6 8 4",
"5 5 3 1 3 5 2 3 4 2 4 5 3 5 3 1 1 2 3 3 2 2 3 1 1",
"4 2 1 5 5 1 4 1 5 3 3 4 1 5 5 2 2 2 5 5 3 5 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8 5 5 5 8 8 6 8 3 0 4 2 2 3 0 4 4 1 1",
"1 4 4 2 4 3 7 5 5 6 3 7 7 4 4 4 7 7 5 7 4 2 2 3 0",
"4 4 1 1 1 4 4 2 4 6 6 7 4 8 8 5 5 5 8 8 6 8 4 5 2",
"6 6 3 3 3 6 6 4 6 5 2 6 6 3 3 3 6 6 4 6 3 7 7 4 4",
"4 7 7 5 7 4 4 1 1 1 4 4 2 4 8 5 5 5 8 8 6 8 5 5 5",
"8 8 6 8 2 2 5 5 3 5 2 5 5 3 5 5 5 3 5 8 6 8 6 8 6"}
Returns: { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }
49 rows of 50 in sums, generating an answer of length 50. Maximal number of elements.
Submissions are judged against all 104 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class PairwiseSums with a public method vector<int> reverse(vector<string> sums) · 104 test cases · 2 s / 256 MB per case