How to get n/2-digit combinations from n digit number





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm struggling with this algorithm. It should work like this:



If I input f.e. 6880, my program should output 80 86 80 86 60 68 68.



As you can see, combinations are repeating. That's because it looks at every digit as it is a different object. In my program it's correct.



Here is my code:



public static Set<Integer> get2DCombinations(List<Integer> digits) {
Set<Integer> combinations = new TreeSet<>();

int t = 0;
for(Integer i : digits) {
for (Integer j : digits) {
t = i * 10 + j;
if (t / 10 >= 1) {
combinations.add(t);
}
}
}

return combinations;
}


It returns a specific set of combinations where all combinations have 2 digits.



It works perfectly, but only with 4-digit numbers. Of course, I can use one more for-each loops, but is there a way to automate it?



So if I input 6-digit number it should output all possible 3-digit combinations of its digits, and if I input 8-digit number, it should output all possible 4-digit combinations. Input numbers always have even amount of digits.



Could you please point out for me how to do so?










share|improve this question




















  • 1





    Those are not all the combinations, you are missing some. Here's the full list : [88, 68, 06, 80, 08, 60, 86]

    – Schidu Luca
    Jan 3 at 11:28











  • Are there any other rules involved?

    – Schidu Luca
    Jan 3 at 11:28











  • Thank you. It was bug in my code. if (i.equals(j)) was useless, but I did it so the same digit couldn't combine with itself. Code works fine without it though. Edited.

    – nklymok
    Jan 3 at 11:38


















1















I'm struggling with this algorithm. It should work like this:



If I input f.e. 6880, my program should output 80 86 80 86 60 68 68.



As you can see, combinations are repeating. That's because it looks at every digit as it is a different object. In my program it's correct.



Here is my code:



public static Set<Integer> get2DCombinations(List<Integer> digits) {
Set<Integer> combinations = new TreeSet<>();

int t = 0;
for(Integer i : digits) {
for (Integer j : digits) {
t = i * 10 + j;
if (t / 10 >= 1) {
combinations.add(t);
}
}
}

return combinations;
}


It returns a specific set of combinations where all combinations have 2 digits.



It works perfectly, but only with 4-digit numbers. Of course, I can use one more for-each loops, but is there a way to automate it?



So if I input 6-digit number it should output all possible 3-digit combinations of its digits, and if I input 8-digit number, it should output all possible 4-digit combinations. Input numbers always have even amount of digits.



Could you please point out for me how to do so?










share|improve this question




















  • 1





    Those are not all the combinations, you are missing some. Here's the full list : [88, 68, 06, 80, 08, 60, 86]

    – Schidu Luca
    Jan 3 at 11:28











  • Are there any other rules involved?

    – Schidu Luca
    Jan 3 at 11:28











  • Thank you. It was bug in my code. if (i.equals(j)) was useless, but I did it so the same digit couldn't combine with itself. Code works fine without it though. Edited.

    – nklymok
    Jan 3 at 11:38














1












1








1








I'm struggling with this algorithm. It should work like this:



If I input f.e. 6880, my program should output 80 86 80 86 60 68 68.



As you can see, combinations are repeating. That's because it looks at every digit as it is a different object. In my program it's correct.



Here is my code:



public static Set<Integer> get2DCombinations(List<Integer> digits) {
Set<Integer> combinations = new TreeSet<>();

int t = 0;
for(Integer i : digits) {
for (Integer j : digits) {
t = i * 10 + j;
if (t / 10 >= 1) {
combinations.add(t);
}
}
}

return combinations;
}


It returns a specific set of combinations where all combinations have 2 digits.



It works perfectly, but only with 4-digit numbers. Of course, I can use one more for-each loops, but is there a way to automate it?



So if I input 6-digit number it should output all possible 3-digit combinations of its digits, and if I input 8-digit number, it should output all possible 4-digit combinations. Input numbers always have even amount of digits.



Could you please point out for me how to do so?










share|improve this question
















I'm struggling with this algorithm. It should work like this:



If I input f.e. 6880, my program should output 80 86 80 86 60 68 68.



As you can see, combinations are repeating. That's because it looks at every digit as it is a different object. In my program it's correct.



Here is my code:



public static Set<Integer> get2DCombinations(List<Integer> digits) {
Set<Integer> combinations = new TreeSet<>();

int t = 0;
for(Integer i : digits) {
for (Integer j : digits) {
t = i * 10 + j;
if (t / 10 >= 1) {
combinations.add(t);
}
}
}

return combinations;
}


It returns a specific set of combinations where all combinations have 2 digits.



It works perfectly, but only with 4-digit numbers. Of course, I can use one more for-each loops, but is there a way to automate it?



So if I input 6-digit number it should output all possible 3-digit combinations of its digits, and if I input 8-digit number, it should output all possible 4-digit combinations. Input numbers always have even amount of digits.



Could you please point out for me how to do so?







java combinations combinatorics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 11:38







nklymok

















asked Jan 3 at 11:10









nklymoknklymok

208




208








  • 1





    Those are not all the combinations, you are missing some. Here's the full list : [88, 68, 06, 80, 08, 60, 86]

    – Schidu Luca
    Jan 3 at 11:28











  • Are there any other rules involved?

    – Schidu Luca
    Jan 3 at 11:28











  • Thank you. It was bug in my code. if (i.equals(j)) was useless, but I did it so the same digit couldn't combine with itself. Code works fine without it though. Edited.

    – nklymok
    Jan 3 at 11:38














  • 1





    Those are not all the combinations, you are missing some. Here's the full list : [88, 68, 06, 80, 08, 60, 86]

    – Schidu Luca
    Jan 3 at 11:28











  • Are there any other rules involved?

    – Schidu Luca
    Jan 3 at 11:28











  • Thank you. It was bug in my code. if (i.equals(j)) was useless, but I did it so the same digit couldn't combine with itself. Code works fine without it though. Edited.

    – nklymok
    Jan 3 at 11:38








1




1





Those are not all the combinations, you are missing some. Here's the full list : [88, 68, 06, 80, 08, 60, 86]

– Schidu Luca
Jan 3 at 11:28





Those are not all the combinations, you are missing some. Here's the full list : [88, 68, 06, 80, 08, 60, 86]

– Schidu Luca
Jan 3 at 11:28













Are there any other rules involved?

– Schidu Luca
Jan 3 at 11:28





Are there any other rules involved?

– Schidu Luca
Jan 3 at 11:28













Thank you. It was bug in my code. if (i.equals(j)) was useless, but I did it so the same digit couldn't combine with itself. Code works fine without it though. Edited.

– nklymok
Jan 3 at 11:38





Thank you. It was bug in my code. if (i.equals(j)) was useless, but I did it so the same digit couldn't combine with itself. Code works fine without it though. Edited.

– nklymok
Jan 3 at 11:38












2 Answers
2






active

oldest

votes


















1














You need a recursive program that will generate all the combinations for your input. Here's a solution of mine. My method accepts a String as input (it's just shorted program and easier, you can adapt it to your needs):



public static Set<String> get2DCombinations(String input) {
return backtracking("", input, input.length() / 2) ;
}

public static Set<String> backtracking(String actual, String remaining, int length) {
if (actual.length() == length) {
return new HashSet<>(Arrays.asList(actual));
}

Set<String> result = new HashSet<>();
for(int i = 0; i < remaining.length(); i++) {
result.addAll(backtracking(actual + remaining.charAt(i), remaining.substring(0, i) + remaining.substring(i + 1), length));
}
return result;
}


And you call the method like so:



System.out.println(get2DCombinations(input));


Result:



[88, 68, 06, 80, 08, 60, 86]


As I mentioned in the comment, your are missing some of the combinations. This solution generates all of them.






share|improve this answer































    0














    Try calculating n / 2 first. So, if n is 6, then n / 2 = 3. Then you know before you start fining the combinations that you are looking for combinations of 3 digits.
    Then you want to find the right algorithm to find the combinations. Part of problem solving is breaking down problems to smaller problems. That is what I did here.
    I can not solve it for you, however, because it is better for you to solve yourself, and second, there details that you dind't provide so it is hard to give the right solution.






    share|improve this answer
























      Your Answer






      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: "1"
      };
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021148%2fhow-to-get-n-2-digit-combinations-from-n-digit-number%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You need a recursive program that will generate all the combinations for your input. Here's a solution of mine. My method accepts a String as input (it's just shorted program and easier, you can adapt it to your needs):



      public static Set<String> get2DCombinations(String input) {
      return backtracking("", input, input.length() / 2) ;
      }

      public static Set<String> backtracking(String actual, String remaining, int length) {
      if (actual.length() == length) {
      return new HashSet<>(Arrays.asList(actual));
      }

      Set<String> result = new HashSet<>();
      for(int i = 0; i < remaining.length(); i++) {
      result.addAll(backtracking(actual + remaining.charAt(i), remaining.substring(0, i) + remaining.substring(i + 1), length));
      }
      return result;
      }


      And you call the method like so:



      System.out.println(get2DCombinations(input));


      Result:



      [88, 68, 06, 80, 08, 60, 86]


      As I mentioned in the comment, your are missing some of the combinations. This solution generates all of them.






      share|improve this answer




























        1














        You need a recursive program that will generate all the combinations for your input. Here's a solution of mine. My method accepts a String as input (it's just shorted program and easier, you can adapt it to your needs):



        public static Set<String> get2DCombinations(String input) {
        return backtracking("", input, input.length() / 2) ;
        }

        public static Set<String> backtracking(String actual, String remaining, int length) {
        if (actual.length() == length) {
        return new HashSet<>(Arrays.asList(actual));
        }

        Set<String> result = new HashSet<>();
        for(int i = 0; i < remaining.length(); i++) {
        result.addAll(backtracking(actual + remaining.charAt(i), remaining.substring(0, i) + remaining.substring(i + 1), length));
        }
        return result;
        }


        And you call the method like so:



        System.out.println(get2DCombinations(input));


        Result:



        [88, 68, 06, 80, 08, 60, 86]


        As I mentioned in the comment, your are missing some of the combinations. This solution generates all of them.






        share|improve this answer


























          1












          1








          1







          You need a recursive program that will generate all the combinations for your input. Here's a solution of mine. My method accepts a String as input (it's just shorted program and easier, you can adapt it to your needs):



          public static Set<String> get2DCombinations(String input) {
          return backtracking("", input, input.length() / 2) ;
          }

          public static Set<String> backtracking(String actual, String remaining, int length) {
          if (actual.length() == length) {
          return new HashSet<>(Arrays.asList(actual));
          }

          Set<String> result = new HashSet<>();
          for(int i = 0; i < remaining.length(); i++) {
          result.addAll(backtracking(actual + remaining.charAt(i), remaining.substring(0, i) + remaining.substring(i + 1), length));
          }
          return result;
          }


          And you call the method like so:



          System.out.println(get2DCombinations(input));


          Result:



          [88, 68, 06, 80, 08, 60, 86]


          As I mentioned in the comment, your are missing some of the combinations. This solution generates all of them.






          share|improve this answer













          You need a recursive program that will generate all the combinations for your input. Here's a solution of mine. My method accepts a String as input (it's just shorted program and easier, you can adapt it to your needs):



          public static Set<String> get2DCombinations(String input) {
          return backtracking("", input, input.length() / 2) ;
          }

          public static Set<String> backtracking(String actual, String remaining, int length) {
          if (actual.length() == length) {
          return new HashSet<>(Arrays.asList(actual));
          }

          Set<String> result = new HashSet<>();
          for(int i = 0; i < remaining.length(); i++) {
          result.addAll(backtracking(actual + remaining.charAt(i), remaining.substring(0, i) + remaining.substring(i + 1), length));
          }
          return result;
          }


          And you call the method like so:



          System.out.println(get2DCombinations(input));


          Result:



          [88, 68, 06, 80, 08, 60, 86]


          As I mentioned in the comment, your are missing some of the combinations. This solution generates all of them.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 11:36









          Schidu LucaSchidu Luca

          3,040522




          3,040522

























              0














              Try calculating n / 2 first. So, if n is 6, then n / 2 = 3. Then you know before you start fining the combinations that you are looking for combinations of 3 digits.
              Then you want to find the right algorithm to find the combinations. Part of problem solving is breaking down problems to smaller problems. That is what I did here.
              I can not solve it for you, however, because it is better for you to solve yourself, and second, there details that you dind't provide so it is hard to give the right solution.






              share|improve this answer




























                0














                Try calculating n / 2 first. So, if n is 6, then n / 2 = 3. Then you know before you start fining the combinations that you are looking for combinations of 3 digits.
                Then you want to find the right algorithm to find the combinations. Part of problem solving is breaking down problems to smaller problems. That is what I did here.
                I can not solve it for you, however, because it is better for you to solve yourself, and second, there details that you dind't provide so it is hard to give the right solution.






                share|improve this answer


























                  0












                  0








                  0







                  Try calculating n / 2 first. So, if n is 6, then n / 2 = 3. Then you know before you start fining the combinations that you are looking for combinations of 3 digits.
                  Then you want to find the right algorithm to find the combinations. Part of problem solving is breaking down problems to smaller problems. That is what I did here.
                  I can not solve it for you, however, because it is better for you to solve yourself, and second, there details that you dind't provide so it is hard to give the right solution.






                  share|improve this answer













                  Try calculating n / 2 first. So, if n is 6, then n / 2 = 3. Then you know before you start fining the combinations that you are looking for combinations of 3 digits.
                  Then you want to find the right algorithm to find the combinations. Part of problem solving is breaking down problems to smaller problems. That is what I did here.
                  I can not solve it for you, however, because it is better for you to solve yourself, and second, there details that you dind't provide so it is hard to give the right solution.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 11:29









                  jaxjax

                  452623




                  452623






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021148%2fhow-to-get-n-2-digit-combinations-from-n-digit-number%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith