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;
}
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
add a comment |
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
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
add a comment |
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
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
java combinations combinatorics
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
add a comment |
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
});
}
});
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
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 3 at 11:36


Schidu LucaSchidu Luca
3,040522
3,040522
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 3 at 11:29
jaxjax
452623
452623
add a comment |
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
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