Find sum of 3 that total a target from a List

The name of the pictureThe name of the pictureThe name of the pictureClash 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)







share|improve this question



























    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)







    share|improve this question























      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)







      share|improve this question













      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)









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 11 at 17:39









      200_success

      123k14142399




      123k14142399









      asked Mar 25 at 14:42









      paparazzo

      4,8131730




      4,8131730




















          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...






          share|improve this answer























          • 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










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























          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...






          share|improve this answer























          • 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














          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...






          share|improve this answer























          • 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












          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...






          share|improve this answer















          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...







          share|improve this answer















          share|improve this answer



          share|improve this answer








          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
















          • 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












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods