Recursion Problem - given array n and a number k
Given an array size n, and a positive number max(max represent the range of the numbers that we can use to place in the array).
I would like to count how many combinations of sorted numbers I can place in the array.
For example :
If n = 3, max = 2
.(the only numbers we can use is 1/2 as max is 2) so there are 4 combinations of sorted arrays
1. {1,1,1}
2. {1,1,2}
3. {1,2,2}
4. {2,2,2}
I wrote some code and succeed to pass this specific example but any other example that max > 2
doesn't return the correct answer.
the problem as I identify it is when the recursion reaches the last index it doesn't try a third number it just folds back.
my code :
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if(numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if(index == n) {
return 1;
}
int sortTwo = howManySorted(n, max, index+1, numToMax, numToMax);
int sortOne = howManySorted(n, max, index+1, numToMax+1, numToMax);
return ((sortOne+sortTwo));
}
public static int howManySorted(int n, int max) {
return howManySorted(n, max, 0, 1, 0);
}
java sorting recursion
add a comment |
Given an array size n, and a positive number max(max represent the range of the numbers that we can use to place in the array).
I would like to count how many combinations of sorted numbers I can place in the array.
For example :
If n = 3, max = 2
.(the only numbers we can use is 1/2 as max is 2) so there are 4 combinations of sorted arrays
1. {1,1,1}
2. {1,1,2}
3. {1,2,2}
4. {2,2,2}
I wrote some code and succeed to pass this specific example but any other example that max > 2
doesn't return the correct answer.
the problem as I identify it is when the recursion reaches the last index it doesn't try a third number it just folds back.
my code :
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if(numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if(index == n) {
return 1;
}
int sortTwo = howManySorted(n, max, index+1, numToMax, numToMax);
int sortOne = howManySorted(n, max, index+1, numToMax+1, numToMax);
return ((sortOne+sortTwo));
}
public static int howManySorted(int n, int max) {
return howManySorted(n, max, 0, 1, 0);
}
java sorting recursion
1
Curious: When cannumToMax < prevNum
ever be true?
– Andreas
Nov 12 '18 at 15:15
You only recurse twice in the code, i.e. the next number can only be same as previous or one higher, which means that formax > 2
you won't find solutions such as1,1,5
, because that is a skip of 4.
– Andreas
Nov 12 '18 at 15:16
Yes, I understand that that's the reason I posted here (:
– omrib40
Nov 12 '18 at 15:18
FYI: Parameterindex
is redundant. Instead of countingindex
up from0
ton
, count downn
to0
in the recursive calls.
– Andreas
Nov 12 '18 at 15:18
So add a loop around the recursive call, looping fromnumToMax
tomax
. --- Also, given my first comment, parameterprevNum
is redundant. Get rid of it.
– Andreas
Nov 12 '18 at 15:20
add a comment |
Given an array size n, and a positive number max(max represent the range of the numbers that we can use to place in the array).
I would like to count how many combinations of sorted numbers I can place in the array.
For example :
If n = 3, max = 2
.(the only numbers we can use is 1/2 as max is 2) so there are 4 combinations of sorted arrays
1. {1,1,1}
2. {1,1,2}
3. {1,2,2}
4. {2,2,2}
I wrote some code and succeed to pass this specific example but any other example that max > 2
doesn't return the correct answer.
the problem as I identify it is when the recursion reaches the last index it doesn't try a third number it just folds back.
my code :
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if(numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if(index == n) {
return 1;
}
int sortTwo = howManySorted(n, max, index+1, numToMax, numToMax);
int sortOne = howManySorted(n, max, index+1, numToMax+1, numToMax);
return ((sortOne+sortTwo));
}
public static int howManySorted(int n, int max) {
return howManySorted(n, max, 0, 1, 0);
}
java sorting recursion
Given an array size n, and a positive number max(max represent the range of the numbers that we can use to place in the array).
I would like to count how many combinations of sorted numbers I can place in the array.
For example :
If n = 3, max = 2
.(the only numbers we can use is 1/2 as max is 2) so there are 4 combinations of sorted arrays
1. {1,1,1}
2. {1,1,2}
3. {1,2,2}
4. {2,2,2}
I wrote some code and succeed to pass this specific example but any other example that max > 2
doesn't return the correct answer.
the problem as I identify it is when the recursion reaches the last index it doesn't try a third number it just folds back.
my code :
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if(numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if(index == n) {
return 1;
}
int sortTwo = howManySorted(n, max, index+1, numToMax, numToMax);
int sortOne = howManySorted(n, max, index+1, numToMax+1, numToMax);
return ((sortOne+sortTwo));
}
public static int howManySorted(int n, int max) {
return howManySorted(n, max, 0, 1, 0);
}
java sorting recursion
java sorting recursion
edited Nov 21 '18 at 9:17
f_puras
2,20342231
2,20342231
asked Nov 12 '18 at 15:10
omrib40
513
513
1
Curious: When cannumToMax < prevNum
ever be true?
– Andreas
Nov 12 '18 at 15:15
You only recurse twice in the code, i.e. the next number can only be same as previous or one higher, which means that formax > 2
you won't find solutions such as1,1,5
, because that is a skip of 4.
– Andreas
Nov 12 '18 at 15:16
Yes, I understand that that's the reason I posted here (:
– omrib40
Nov 12 '18 at 15:18
FYI: Parameterindex
is redundant. Instead of countingindex
up from0
ton
, count downn
to0
in the recursive calls.
– Andreas
Nov 12 '18 at 15:18
So add a loop around the recursive call, looping fromnumToMax
tomax
. --- Also, given my first comment, parameterprevNum
is redundant. Get rid of it.
– Andreas
Nov 12 '18 at 15:20
add a comment |
1
Curious: When cannumToMax < prevNum
ever be true?
– Andreas
Nov 12 '18 at 15:15
You only recurse twice in the code, i.e. the next number can only be same as previous or one higher, which means that formax > 2
you won't find solutions such as1,1,5
, because that is a skip of 4.
– Andreas
Nov 12 '18 at 15:16
Yes, I understand that that's the reason I posted here (:
– omrib40
Nov 12 '18 at 15:18
FYI: Parameterindex
is redundant. Instead of countingindex
up from0
ton
, count downn
to0
in the recursive calls.
– Andreas
Nov 12 '18 at 15:18
So add a loop around the recursive call, looping fromnumToMax
tomax
. --- Also, given my first comment, parameterprevNum
is redundant. Get rid of it.
– Andreas
Nov 12 '18 at 15:20
1
1
Curious: When can
numToMax < prevNum
ever be true?– Andreas
Nov 12 '18 at 15:15
Curious: When can
numToMax < prevNum
ever be true?– Andreas
Nov 12 '18 at 15:15
You only recurse twice in the code, i.e. the next number can only be same as previous or one higher, which means that for
max > 2
you won't find solutions such as 1,1,5
, because that is a skip of 4.– Andreas
Nov 12 '18 at 15:16
You only recurse twice in the code, i.e. the next number can only be same as previous or one higher, which means that for
max > 2
you won't find solutions such as 1,1,5
, because that is a skip of 4.– Andreas
Nov 12 '18 at 15:16
Yes, I understand that that's the reason I posted here (:
– omrib40
Nov 12 '18 at 15:18
Yes, I understand that that's the reason I posted here (:
– omrib40
Nov 12 '18 at 15:18
FYI: Parameter
index
is redundant. Instead of counting index
up from 0
to n
, count down n
to 0
in the recursive calls.– Andreas
Nov 12 '18 at 15:18
FYI: Parameter
index
is redundant. Instead of counting index
up from 0
to n
, count down n
to 0
in the recursive calls.– Andreas
Nov 12 '18 at 15:18
So add a loop around the recursive call, looping from
numToMax
to max
. --- Also, given my first comment, parameter prevNum
is redundant. Get rid of it.– Andreas
Nov 12 '18 at 15:20
So add a loop around the recursive call, looping from
numToMax
to max
. --- Also, given my first comment, parameter prevNum
is redundant. Get rid of it.– Andreas
Nov 12 '18 at 15:20
add a comment |
3 Answers
3
active
oldest
votes
I think you would need to change your two recursive calls (this is why it only reaches value 2) and do as many calls as your max
parameter:
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if (numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if (index == n) {
return 1;
}
int result = 0;
for (int i = 0; i < max; i++)
result += howManySorted(n, max, index + 1, numToMax + i, numToMax);
return result;
}
add a comment |
I believe you can simplify your answer to something like this
private static long howManySorted(int length, int min, int max) {
if (length == 1) {
return max - min + 1;
}
// if (min == max) {
// return 1;
// }
long result = 0;
for (int i = min; i <= max; i++) {
result += howManySorted(length - 1, i, max);
}
return result;
}
public static long howManySorted(int length, int max) {
if ((length < 1) || (max < 1)) {
throw new IllegalArgumentException();
}
return howManySorted(length, 1, max);
}
Client should call the public
method.
So as you can see terminate conditions are when remaining length
is 1, or min
reaches max
. Even removing the second terminate condition doesn't change the result, but can improve the performance and number of recursions.
add a comment |
Just test my code, I think it figures out your problem:
class Test {
private static int howManySorted(int n, int max) {
//Better time complexity if u use dynamic programming rather than recursion.
if (n == 0) return 1;
int res = 0; // "res" can be a very large.
for (int i = n; i >= 1; i--) {
for (int j = max; j >= 1;j--) {
res += howManySorted(i-1, j-1);
}
}
return res;
}
public static void main(String args) {
System.out.println(howManySorted(3, 2));
}
}
This code will run faster if you use dynamic programming and be careful about the answer, it could be a very large integer.
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%2f53264992%2frecursion-problem-given-array-n-and-a-number-k%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you would need to change your two recursive calls (this is why it only reaches value 2) and do as many calls as your max
parameter:
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if (numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if (index == n) {
return 1;
}
int result = 0;
for (int i = 0; i < max; i++)
result += howManySorted(n, max, index + 1, numToMax + i, numToMax);
return result;
}
add a comment |
I think you would need to change your two recursive calls (this is why it only reaches value 2) and do as many calls as your max
parameter:
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if (numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if (index == n) {
return 1;
}
int result = 0;
for (int i = 0; i < max; i++)
result += howManySorted(n, max, index + 1, numToMax + i, numToMax);
return result;
}
add a comment |
I think you would need to change your two recursive calls (this is why it only reaches value 2) and do as many calls as your max
parameter:
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if (numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if (index == n) {
return 1;
}
int result = 0;
for (int i = 0; i < max; i++)
result += howManySorted(n, max, index + 1, numToMax + i, numToMax);
return result;
}
I think you would need to change your two recursive calls (this is why it only reaches value 2) and do as many calls as your max
parameter:
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if (numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if (index == n) {
return 1;
}
int result = 0;
for (int i = 0; i < max; i++)
result += howManySorted(n, max, index + 1, numToMax + i, numToMax);
return result;
}
answered Dec 8 '18 at 1:40
y.luis
1,0441224
1,0441224
add a comment |
add a comment |
I believe you can simplify your answer to something like this
private static long howManySorted(int length, int min, int max) {
if (length == 1) {
return max - min + 1;
}
// if (min == max) {
// return 1;
// }
long result = 0;
for (int i = min; i <= max; i++) {
result += howManySorted(length - 1, i, max);
}
return result;
}
public static long howManySorted(int length, int max) {
if ((length < 1) || (max < 1)) {
throw new IllegalArgumentException();
}
return howManySorted(length, 1, max);
}
Client should call the public
method.
So as you can see terminate conditions are when remaining length
is 1, or min
reaches max
. Even removing the second terminate condition doesn't change the result, but can improve the performance and number of recursions.
add a comment |
I believe you can simplify your answer to something like this
private static long howManySorted(int length, int min, int max) {
if (length == 1) {
return max - min + 1;
}
// if (min == max) {
// return 1;
// }
long result = 0;
for (int i = min; i <= max; i++) {
result += howManySorted(length - 1, i, max);
}
return result;
}
public static long howManySorted(int length, int max) {
if ((length < 1) || (max < 1)) {
throw new IllegalArgumentException();
}
return howManySorted(length, 1, max);
}
Client should call the public
method.
So as you can see terminate conditions are when remaining length
is 1, or min
reaches max
. Even removing the second terminate condition doesn't change the result, but can improve the performance and number of recursions.
add a comment |
I believe you can simplify your answer to something like this
private static long howManySorted(int length, int min, int max) {
if (length == 1) {
return max - min + 1;
}
// if (min == max) {
// return 1;
// }
long result = 0;
for (int i = min; i <= max; i++) {
result += howManySorted(length - 1, i, max);
}
return result;
}
public static long howManySorted(int length, int max) {
if ((length < 1) || (max < 1)) {
throw new IllegalArgumentException();
}
return howManySorted(length, 1, max);
}
Client should call the public
method.
So as you can see terminate conditions are when remaining length
is 1, or min
reaches max
. Even removing the second terminate condition doesn't change the result, but can improve the performance and number of recursions.
I believe you can simplify your answer to something like this
private static long howManySorted(int length, int min, int max) {
if (length == 1) {
return max - min + 1;
}
// if (min == max) {
// return 1;
// }
long result = 0;
for (int i = min; i <= max; i++) {
result += howManySorted(length - 1, i, max);
}
return result;
}
public static long howManySorted(int length, int max) {
if ((length < 1) || (max < 1)) {
throw new IllegalArgumentException();
}
return howManySorted(length, 1, max);
}
Client should call the public
method.
So as you can see terminate conditions are when remaining length
is 1, or min
reaches max
. Even removing the second terminate condition doesn't change the result, but can improve the performance and number of recursions.
edited Dec 8 '18 at 2:28
answered Dec 8 '18 at 2:09
Amir Pashazadeh
5,88212349
5,88212349
add a comment |
add a comment |
Just test my code, I think it figures out your problem:
class Test {
private static int howManySorted(int n, int max) {
//Better time complexity if u use dynamic programming rather than recursion.
if (n == 0) return 1;
int res = 0; // "res" can be a very large.
for (int i = n; i >= 1; i--) {
for (int j = max; j >= 1;j--) {
res += howManySorted(i-1, j-1);
}
}
return res;
}
public static void main(String args) {
System.out.println(howManySorted(3, 2));
}
}
This code will run faster if you use dynamic programming and be careful about the answer, it could be a very large integer.
add a comment |
Just test my code, I think it figures out your problem:
class Test {
private static int howManySorted(int n, int max) {
//Better time complexity if u use dynamic programming rather than recursion.
if (n == 0) return 1;
int res = 0; // "res" can be a very large.
for (int i = n; i >= 1; i--) {
for (int j = max; j >= 1;j--) {
res += howManySorted(i-1, j-1);
}
}
return res;
}
public static void main(String args) {
System.out.println(howManySorted(3, 2));
}
}
This code will run faster if you use dynamic programming and be careful about the answer, it could be a very large integer.
add a comment |
Just test my code, I think it figures out your problem:
class Test {
private static int howManySorted(int n, int max) {
//Better time complexity if u use dynamic programming rather than recursion.
if (n == 0) return 1;
int res = 0; // "res" can be a very large.
for (int i = n; i >= 1; i--) {
for (int j = max; j >= 1;j--) {
res += howManySorted(i-1, j-1);
}
}
return res;
}
public static void main(String args) {
System.out.println(howManySorted(3, 2));
}
}
This code will run faster if you use dynamic programming and be careful about the answer, it could be a very large integer.
Just test my code, I think it figures out your problem:
class Test {
private static int howManySorted(int n, int max) {
//Better time complexity if u use dynamic programming rather than recursion.
if (n == 0) return 1;
int res = 0; // "res" can be a very large.
for (int i = n; i >= 1; i--) {
for (int j = max; j >= 1;j--) {
res += howManySorted(i-1, j-1);
}
}
return res;
}
public static void main(String args) {
System.out.println(howManySorted(3, 2));
}
}
This code will run faster if you use dynamic programming and be careful about the answer, it could be a very large integer.
answered Dec 8 '18 at 2:56
Richaro311
275
275
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53264992%2frecursion-problem-given-array-n-and-a-number-k%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
Curious: When can
numToMax < prevNum
ever be true?– Andreas
Nov 12 '18 at 15:15
You only recurse twice in the code, i.e. the next number can only be same as previous or one higher, which means that for
max > 2
you won't find solutions such as1,1,5
, because that is a skip of 4.– Andreas
Nov 12 '18 at 15:16
Yes, I understand that that's the reason I posted here (:
– omrib40
Nov 12 '18 at 15:18
FYI: Parameter
index
is redundant. Instead of countingindex
up from0
ton
, count downn
to0
in the recursive calls.– Andreas
Nov 12 '18 at 15:18
So add a loop around the recursive call, looping from
numToMax
tomax
. --- Also, given my first comment, parameterprevNum
is redundant. Get rid of it.– Andreas
Nov 12 '18 at 15:20