Find sum of 3 that total a target from a List

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
A related Java question got me curios.
All unique combinations (not permutations) of 3 values that sum to a target from a list of integers.
Values can duplicate in the list but are only used once.
By sorting the input the evaluation is able to take shortcuts.
Feedback on both code and speed please.
Assume all input and sum >= 0.
public static List<List<int>> FindThreeSum(List<int> input, int sum = 24)
c# algorithm .net k-sum
add a comment |Â
up vote
2
down vote
favorite
A related Java question got me curios.
All unique combinations (not permutations) of 3 values that sum to a target from a list of integers.
Values can duplicate in the list but are only used once.
By sorting the input the evaluation is able to take shortcuts.
Feedback on both code and speed please.
Assume all input and sum >= 0.
public static List<List<int>> FindThreeSum(List<int> input, int sum = 24)
c# algorithm .net k-sum
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
A related Java question got me curios.
All unique combinations (not permutations) of 3 values that sum to a target from a list of integers.
Values can duplicate in the list but are only used once.
By sorting the input the evaluation is able to take shortcuts.
Feedback on both code and speed please.
Assume all input and sum >= 0.
public static List<List<int>> FindThreeSum(List<int> input, int sum = 24)
c# algorithm .net k-sum
A related Java question got me curios.
All unique combinations (not permutations) of 3 values that sum to a target from a list of integers.
Values can duplicate in the list but are only used once.
By sorting the input the evaluation is able to take shortcuts.
Feedback on both code and speed please.
Assume all input and sum >= 0.
public static List<List<int>> FindThreeSum(List<int> input, int sum = 24)
c# algorithm .net k-sum
edited Jul 11 at 17:39
200_success
123k14142399
123k14142399
asked Mar 25 at 14:42
paparazzo
4,8131730
4,8131730
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
In general it looks OK to me, but you could maybe consider the following:
1) Return a IEnumerable<int> instead of List<List<int>> and then yield the positive results when found:
...
else if (sumSoFar == sum)
yield return new int iValue, jValue, kValue ;
...
2) The names sumI, sumJ, sumK are somewhat misleading because they aren't sums. Better names would be valueI, -J, -K
int valueI;
int valueJ;
int valueK;
3) For the fun of it, you could consider: Because you basically do the same same loop nested three times, it could be a candidate for a recursive function iterating over each addend and yielding all the positive sums. In that way you could generalize the algorithm to handle any number of addends...
3 * maxInput could be bigger than int
â paparazzo
Mar 26 at 8:00
@paparazzo: OK, I see, it's easier your way - changed my answer.
â Henrik Hansen
Mar 26 at 8:16
If I don't get any more answers by tomorrow I will accept.
â paparazzo
Mar 26 at 14:10
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
In general it looks OK to me, but you could maybe consider the following:
1) Return a IEnumerable<int> instead of List<List<int>> and then yield the positive results when found:
...
else if (sumSoFar == sum)
yield return new int iValue, jValue, kValue ;
...
2) The names sumI, sumJ, sumK are somewhat misleading because they aren't sums. Better names would be valueI, -J, -K
int valueI;
int valueJ;
int valueK;
3) For the fun of it, you could consider: Because you basically do the same same loop nested three times, it could be a candidate for a recursive function iterating over each addend and yielding all the positive sums. In that way you could generalize the algorithm to handle any number of addends...
3 * maxInput could be bigger than int
â paparazzo
Mar 26 at 8:00
@paparazzo: OK, I see, it's easier your way - changed my answer.
â Henrik Hansen
Mar 26 at 8:16
If I don't get any more answers by tomorrow I will accept.
â paparazzo
Mar 26 at 14:10
add a comment |Â
up vote
2
down vote
accepted
In general it looks OK to me, but you could maybe consider the following:
1) Return a IEnumerable<int> instead of List<List<int>> and then yield the positive results when found:
...
else if (sumSoFar == sum)
yield return new int iValue, jValue, kValue ;
...
2) The names sumI, sumJ, sumK are somewhat misleading because they aren't sums. Better names would be valueI, -J, -K
int valueI;
int valueJ;
int valueK;
3) For the fun of it, you could consider: Because you basically do the same same loop nested three times, it could be a candidate for a recursive function iterating over each addend and yielding all the positive sums. In that way you could generalize the algorithm to handle any number of addends...
3 * maxInput could be bigger than int
â paparazzo
Mar 26 at 8:00
@paparazzo: OK, I see, it's easier your way - changed my answer.
â Henrik Hansen
Mar 26 at 8:16
If I don't get any more answers by tomorrow I will accept.
â paparazzo
Mar 26 at 14:10
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
In general it looks OK to me, but you could maybe consider the following:
1) Return a IEnumerable<int> instead of List<List<int>> and then yield the positive results when found:
...
else if (sumSoFar == sum)
yield return new int iValue, jValue, kValue ;
...
2) The names sumI, sumJ, sumK are somewhat misleading because they aren't sums. Better names would be valueI, -J, -K
int valueI;
int valueJ;
int valueK;
3) For the fun of it, you could consider: Because you basically do the same same loop nested three times, it could be a candidate for a recursive function iterating over each addend and yielding all the positive sums. In that way you could generalize the algorithm to handle any number of addends...
In general it looks OK to me, but you could maybe consider the following:
1) Return a IEnumerable<int> instead of List<List<int>> and then yield the positive results when found:
...
else if (sumSoFar == sum)
yield return new int iValue, jValue, kValue ;
...
2) The names sumI, sumJ, sumK are somewhat misleading because they aren't sums. Better names would be valueI, -J, -K
int valueI;
int valueJ;
int valueK;
3) For the fun of it, you could consider: Because you basically do the same same loop nested three times, it could be a candidate for a recursive function iterating over each addend and yielding all the positive sums. In that way you could generalize the algorithm to handle any number of addends...
edited Mar 26 at 8:17
answered Mar 26 at 7:27
Henrik Hansen
3,8931417
3,8931417
3 * maxInput could be bigger than int
â paparazzo
Mar 26 at 8:00
@paparazzo: OK, I see, it's easier your way - changed my answer.
â Henrik Hansen
Mar 26 at 8:16
If I don't get any more answers by tomorrow I will accept.
â paparazzo
Mar 26 at 14:10
add a comment |Â
3 * maxInput could be bigger than int
â paparazzo
Mar 26 at 8:00
@paparazzo: OK, I see, it's easier your way - changed my answer.
â Henrik Hansen
Mar 26 at 8:16
If I don't get any more answers by tomorrow I will accept.
â paparazzo
Mar 26 at 14:10
3 * maxInput could be bigger than int
â paparazzo
Mar 26 at 8:00
3 * maxInput could be bigger than int
â paparazzo
Mar 26 at 8:00
@paparazzo: OK, I see, it's easier your way - changed my answer.
â Henrik Hansen
Mar 26 at 8:16
@paparazzo: OK, I see, it's easier your way - changed my answer.
â Henrik Hansen
Mar 26 at 8:16
If I don't get any more answers by tomorrow I will accept.
â paparazzo
Mar 26 at 14:10
If I don't get any more answers by tomorrow I will accept.
â paparazzo
Mar 26 at 14:10
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190441%2ffind-sum-of-3-that-total-a-target-from-a-list%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password