Finding pairs that add up to a given sum, using recursion
$begingroup$
I'm currently tasked with using recursion to find pairs that add up to a given sum. How would I make the function recursiveFixedSumPairs function more efficient and truncate this code? Also, what is the current runtime? I believe it is n since it will go through the array only once always starting at the first index.
public class HW4 {
public static void main(String args) {
int a = {1, 5, 8, 11, 14, 15, 20, 23, 25, 28, 30, 34};
int k;
k = 43;
System.out.println("k = " + k);
findFixedSumPairs(a, k);
}
public static void findFixedSumPairs(int a, int k) {
recursiveFixedSumPairs(a, -1, 0, k);
}
private static void recursiveFixedSumPairs(int array, int subPair1, int index, int k) {
// // pairs whose sum equals k are printed in this method
// if ((array[subPair1] + array[subPair2]) == k) {
// System.out.println("[" + array[subPair1] + ", " + array[subPair2] + "]");
// } else if ((array[subPair1] + array[subPair2]) > k) {
// recursiveFixedSumPairs(array, subPair1, subPair2 - 1, k);
// } else {
// recursiveFixedSumPairs(array, subPair1 + 1, subPair2, k);
// }
if (index == array.length) {
return;
}
else if (index == array.length-1) {
if (subPair1!= -1 && subPair1 + array[index] == k) {
System.out.println(""+subPair1 +" "+ array[index]);
}
else {
return;
}
}
if (subPair1 != -1) {
if (array[index] == k- subPair1) {
System.out.println(""+subPair1 +" "+ array[index]);
return;
} else {
recursiveFixedSumPairs(array, subPair1, index + 1, k);
}
} else {
recursiveFixedSumPairs(array, array[index], index+1, k);
recursiveFixedSumPairs(array,-1, index+1, k);
}
}
}
java recursion complexity k-sum
$endgroup$
migrated from stackoverflow.com 2 hours ago
This question came from our site for professional and enthusiast programmers.
add a comment |
$begingroup$
I'm currently tasked with using recursion to find pairs that add up to a given sum. How would I make the function recursiveFixedSumPairs function more efficient and truncate this code? Also, what is the current runtime? I believe it is n since it will go through the array only once always starting at the first index.
public class HW4 {
public static void main(String args) {
int a = {1, 5, 8, 11, 14, 15, 20, 23, 25, 28, 30, 34};
int k;
k = 43;
System.out.println("k = " + k);
findFixedSumPairs(a, k);
}
public static void findFixedSumPairs(int a, int k) {
recursiveFixedSumPairs(a, -1, 0, k);
}
private static void recursiveFixedSumPairs(int array, int subPair1, int index, int k) {
// // pairs whose sum equals k are printed in this method
// if ((array[subPair1] + array[subPair2]) == k) {
// System.out.println("[" + array[subPair1] + ", " + array[subPair2] + "]");
// } else if ((array[subPair1] + array[subPair2]) > k) {
// recursiveFixedSumPairs(array, subPair1, subPair2 - 1, k);
// } else {
// recursiveFixedSumPairs(array, subPair1 + 1, subPair2, k);
// }
if (index == array.length) {
return;
}
else if (index == array.length-1) {
if (subPair1!= -1 && subPair1 + array[index] == k) {
System.out.println(""+subPair1 +" "+ array[index]);
}
else {
return;
}
}
if (subPair1 != -1) {
if (array[index] == k- subPair1) {
System.out.println(""+subPair1 +" "+ array[index]);
return;
} else {
recursiveFixedSumPairs(array, subPair1, index + 1, k);
}
} else {
recursiveFixedSumPairs(array, array[index], index+1, k);
recursiveFixedSumPairs(array,-1, index+1, k);
}
}
}
java recursion complexity k-sum
$endgroup$
migrated from stackoverflow.com 2 hours ago
This question came from our site for professional and enthusiast programmers.
2
$begingroup$
Does this code work? If so, Code Review might be a better place to post this.
$endgroup$
– Kevin Anderson
3 hours ago
add a comment |
$begingroup$
I'm currently tasked with using recursion to find pairs that add up to a given sum. How would I make the function recursiveFixedSumPairs function more efficient and truncate this code? Also, what is the current runtime? I believe it is n since it will go through the array only once always starting at the first index.
public class HW4 {
public static void main(String args) {
int a = {1, 5, 8, 11, 14, 15, 20, 23, 25, 28, 30, 34};
int k;
k = 43;
System.out.println("k = " + k);
findFixedSumPairs(a, k);
}
public static void findFixedSumPairs(int a, int k) {
recursiveFixedSumPairs(a, -1, 0, k);
}
private static void recursiveFixedSumPairs(int array, int subPair1, int index, int k) {
// // pairs whose sum equals k are printed in this method
// if ((array[subPair1] + array[subPair2]) == k) {
// System.out.println("[" + array[subPair1] + ", " + array[subPair2] + "]");
// } else if ((array[subPair1] + array[subPair2]) > k) {
// recursiveFixedSumPairs(array, subPair1, subPair2 - 1, k);
// } else {
// recursiveFixedSumPairs(array, subPair1 + 1, subPair2, k);
// }
if (index == array.length) {
return;
}
else if (index == array.length-1) {
if (subPair1!= -1 && subPair1 + array[index] == k) {
System.out.println(""+subPair1 +" "+ array[index]);
}
else {
return;
}
}
if (subPair1 != -1) {
if (array[index] == k- subPair1) {
System.out.println(""+subPair1 +" "+ array[index]);
return;
} else {
recursiveFixedSumPairs(array, subPair1, index + 1, k);
}
} else {
recursiveFixedSumPairs(array, array[index], index+1, k);
recursiveFixedSumPairs(array,-1, index+1, k);
}
}
}
java recursion complexity k-sum
$endgroup$
I'm currently tasked with using recursion to find pairs that add up to a given sum. How would I make the function recursiveFixedSumPairs function more efficient and truncate this code? Also, what is the current runtime? I believe it is n since it will go through the array only once always starting at the first index.
public class HW4 {
public static void main(String args) {
int a = {1, 5, 8, 11, 14, 15, 20, 23, 25, 28, 30, 34};
int k;
k = 43;
System.out.println("k = " + k);
findFixedSumPairs(a, k);
}
public static void findFixedSumPairs(int a, int k) {
recursiveFixedSumPairs(a, -1, 0, k);
}
private static void recursiveFixedSumPairs(int array, int subPair1, int index, int k) {
// // pairs whose sum equals k are printed in this method
// if ((array[subPair1] + array[subPair2]) == k) {
// System.out.println("[" + array[subPair1] + ", " + array[subPair2] + "]");
// } else if ((array[subPair1] + array[subPair2]) > k) {
// recursiveFixedSumPairs(array, subPair1, subPair2 - 1, k);
// } else {
// recursiveFixedSumPairs(array, subPair1 + 1, subPair2, k);
// }
if (index == array.length) {
return;
}
else if (index == array.length-1) {
if (subPair1!= -1 && subPair1 + array[index] == k) {
System.out.println(""+subPair1 +" "+ array[index]);
}
else {
return;
}
}
if (subPair1 != -1) {
if (array[index] == k- subPair1) {
System.out.println(""+subPair1 +" "+ array[index]);
return;
} else {
recursiveFixedSumPairs(array, subPair1, index + 1, k);
}
} else {
recursiveFixedSumPairs(array, array[index], index+1, k);
recursiveFixedSumPairs(array,-1, index+1, k);
}
}
}
java recursion complexity k-sum
java recursion complexity k-sum
edited 4 mins ago
200_success
129k15153416
129k15153416
asked 3 hours ago
FanonX
migrated from stackoverflow.com 2 hours ago
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com 2 hours ago
This question came from our site for professional and enthusiast programmers.
2
$begingroup$
Does this code work? If so, Code Review might be a better place to post this.
$endgroup$
– Kevin Anderson
3 hours ago
add a comment |
2
$begingroup$
Does this code work? If so, Code Review might be a better place to post this.
$endgroup$
– Kevin Anderson
3 hours ago
2
2
$begingroup$
Does this code work? If so, Code Review might be a better place to post this.
$endgroup$
– Kevin Anderson
3 hours ago
$begingroup$
Does this code work? If so, Code Review might be a better place to post this.
$endgroup$
– Kevin Anderson
3 hours ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
You made it too hard:
public class HW4 {
public static void main(String args) {
int a = {1, 5, 8, 11, 14, 15, 20, 23, 25, 28, 30, 34};
int k;
k = 43;
System.out.println("k = " + k);
findSumPairs(a, k, 0);
}
private static void findSumPairs(int array, int target, int index) {
for (int i=index+1; i<array.length; i++)
{
if (array[index] + array[i] == target)
{
System.out.println(array[index] + " " + array[i]);
}
}
if (index < array.length -1)
{
findSumPairs(array, target, ++index);
}
}
}
$endgroup$
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fcodereview.stackexchange.com%2fquestions%2f213929%2ffinding-pairs-that-add-up-to-a-given-sum-using-recursion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
You made it too hard:
public class HW4 {
public static void main(String args) {
int a = {1, 5, 8, 11, 14, 15, 20, 23, 25, 28, 30, 34};
int k;
k = 43;
System.out.println("k = " + k);
findSumPairs(a, k, 0);
}
private static void findSumPairs(int array, int target, int index) {
for (int i=index+1; i<array.length; i++)
{
if (array[index] + array[i] == target)
{
System.out.println(array[index] + " " + array[i]);
}
}
if (index < array.length -1)
{
findSumPairs(array, target, ++index);
}
}
}
$endgroup$
add a comment |
$begingroup$
You made it too hard:
public class HW4 {
public static void main(String args) {
int a = {1, 5, 8, 11, 14, 15, 20, 23, 25, 28, 30, 34};
int k;
k = 43;
System.out.println("k = " + k);
findSumPairs(a, k, 0);
}
private static void findSumPairs(int array, int target, int index) {
for (int i=index+1; i<array.length; i++)
{
if (array[index] + array[i] == target)
{
System.out.println(array[index] + " " + array[i]);
}
}
if (index < array.length -1)
{
findSumPairs(array, target, ++index);
}
}
}
$endgroup$
add a comment |
$begingroup$
You made it too hard:
public class HW4 {
public static void main(String args) {
int a = {1, 5, 8, 11, 14, 15, 20, 23, 25, 28, 30, 34};
int k;
k = 43;
System.out.println("k = " + k);
findSumPairs(a, k, 0);
}
private static void findSumPairs(int array, int target, int index) {
for (int i=index+1; i<array.length; i++)
{
if (array[index] + array[i] == target)
{
System.out.println(array[index] + " " + array[i]);
}
}
if (index < array.length -1)
{
findSumPairs(array, target, ++index);
}
}
}
$endgroup$
You made it too hard:
public class HW4 {
public static void main(String args) {
int a = {1, 5, 8, 11, 14, 15, 20, 23, 25, 28, 30, 34};
int k;
k = 43;
System.out.println("k = " + k);
findSumPairs(a, k, 0);
}
private static void findSumPairs(int array, int target, int index) {
for (int i=index+1; i<array.length; i++)
{
if (array[index] + array[i] == target)
{
System.out.println(array[index] + " " + array[i]);
}
}
if (index < array.length -1)
{
findSumPairs(array, target, ++index);
}
}
}
answered 2 hours ago
see sharpersee sharper
101
101
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fcodereview.stackexchange.com%2fquestions%2f213929%2ffinding-pairs-that-add-up-to-a-given-sum-using-recursion%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
2
$begingroup$
Does this code work? If so, Code Review might be a better place to post this.
$endgroup$
– Kevin Anderson
3 hours ago