Having trouble with divide and conquer algorithm for adding consecutive pairs of ints in an array











up vote
3
down vote

favorite












So I am attempting to get my head around the divide and conquer principle and multiple recursive calls in a single method. It's going ok but I have a problem with the output of the method I am writing.



The purpose of the method is to return the sum of all the pairs of consecutive numbers in an array. I am 95% there but am not getting the output I expect and have been banging me head against the desk for ages trying to work out why.



The array is:



int array = { 11, 6, 87, 32, 15, 5, 9, 21 };


and the method is:



public int consecutivePairsSum_DivideAndConquer(int start, int end, int array) {
int leftSum;
int rightSum;
int middle = (start + end) / 2;
if (start == middle) {
return array[middle];
} else {
leftSum = array[start] + array[start + 1];
leftSum += consecutivePairsSum_DivideAndConquer(start, middle, array);
}
if (middle == end) {
return array[end];
} else {
rightSum = array[middle] + array[middle+1];
rightSum += consecutivePairsSum_DivideAndConquer(middle+1, end, array);
}
return leftSum + rightSum;
}


Here's my method call:



System.out.println(rF.consecutivePairsSum_DivideAndConquer(0, array.length-1, array));


I think it must be something to do with how I have split the array but no amount of experimenting is giving me the right output.



Expected output: 340



Actual output: 330



Any suggestions most welcome, this is driving me nuts! :p



ps Any useful links to where I can find a solid online tutorial/good book about recursion would also be great (if that's within the scope of SO seeing how it's not direct help with a programming issue)










share|improve this question






















  • If I remove a number like 5 from the array the sum goes up! Hmm.
    – Joakim Danielson
    Nov 9 at 22:21










  • Oh yeah just did it as well and it went up?! This recursion stuff is really complicated to grasp, especially with multiple branches
    – PumpkinBreath
    Nov 9 at 22:26










  • Sorry for the irrelevant question: Why would you calculate this sum using recursion? This problem doesn't seem to be a good fit for a divide and conquer approach, although it can be done.
    – 0605002
    Nov 9 at 22:43










  • It's a task I've been given as a way to practice divide and conquer algorithms. It seems that a lot of practice q's to understand these concepts aren't particularly practical, they're just there to help your understanding of the principle
    – PumpkinBreath
    Nov 9 at 22:49















up vote
3
down vote

favorite












So I am attempting to get my head around the divide and conquer principle and multiple recursive calls in a single method. It's going ok but I have a problem with the output of the method I am writing.



The purpose of the method is to return the sum of all the pairs of consecutive numbers in an array. I am 95% there but am not getting the output I expect and have been banging me head against the desk for ages trying to work out why.



The array is:



int array = { 11, 6, 87, 32, 15, 5, 9, 21 };


and the method is:



public int consecutivePairsSum_DivideAndConquer(int start, int end, int array) {
int leftSum;
int rightSum;
int middle = (start + end) / 2;
if (start == middle) {
return array[middle];
} else {
leftSum = array[start] + array[start + 1];
leftSum += consecutivePairsSum_DivideAndConquer(start, middle, array);
}
if (middle == end) {
return array[end];
} else {
rightSum = array[middle] + array[middle+1];
rightSum += consecutivePairsSum_DivideAndConquer(middle+1, end, array);
}
return leftSum + rightSum;
}


Here's my method call:



System.out.println(rF.consecutivePairsSum_DivideAndConquer(0, array.length-1, array));


I think it must be something to do with how I have split the array but no amount of experimenting is giving me the right output.



Expected output: 340



Actual output: 330



Any suggestions most welcome, this is driving me nuts! :p



ps Any useful links to where I can find a solid online tutorial/good book about recursion would also be great (if that's within the scope of SO seeing how it's not direct help with a programming issue)










share|improve this question






















  • If I remove a number like 5 from the array the sum goes up! Hmm.
    – Joakim Danielson
    Nov 9 at 22:21










  • Oh yeah just did it as well and it went up?! This recursion stuff is really complicated to grasp, especially with multiple branches
    – PumpkinBreath
    Nov 9 at 22:26










  • Sorry for the irrelevant question: Why would you calculate this sum using recursion? This problem doesn't seem to be a good fit for a divide and conquer approach, although it can be done.
    – 0605002
    Nov 9 at 22:43










  • It's a task I've been given as a way to practice divide and conquer algorithms. It seems that a lot of practice q's to understand these concepts aren't particularly practical, they're just there to help your understanding of the principle
    – PumpkinBreath
    Nov 9 at 22:49













up vote
3
down vote

favorite









up vote
3
down vote

favorite











So I am attempting to get my head around the divide and conquer principle and multiple recursive calls in a single method. It's going ok but I have a problem with the output of the method I am writing.



The purpose of the method is to return the sum of all the pairs of consecutive numbers in an array. I am 95% there but am not getting the output I expect and have been banging me head against the desk for ages trying to work out why.



The array is:



int array = { 11, 6, 87, 32, 15, 5, 9, 21 };


and the method is:



public int consecutivePairsSum_DivideAndConquer(int start, int end, int array) {
int leftSum;
int rightSum;
int middle = (start + end) / 2;
if (start == middle) {
return array[middle];
} else {
leftSum = array[start] + array[start + 1];
leftSum += consecutivePairsSum_DivideAndConquer(start, middle, array);
}
if (middle == end) {
return array[end];
} else {
rightSum = array[middle] + array[middle+1];
rightSum += consecutivePairsSum_DivideAndConquer(middle+1, end, array);
}
return leftSum + rightSum;
}


Here's my method call:



System.out.println(rF.consecutivePairsSum_DivideAndConquer(0, array.length-1, array));


I think it must be something to do with how I have split the array but no amount of experimenting is giving me the right output.



Expected output: 340



Actual output: 330



Any suggestions most welcome, this is driving me nuts! :p



ps Any useful links to where I can find a solid online tutorial/good book about recursion would also be great (if that's within the scope of SO seeing how it's not direct help with a programming issue)










share|improve this question













So I am attempting to get my head around the divide and conquer principle and multiple recursive calls in a single method. It's going ok but I have a problem with the output of the method I am writing.



The purpose of the method is to return the sum of all the pairs of consecutive numbers in an array. I am 95% there but am not getting the output I expect and have been banging me head against the desk for ages trying to work out why.



The array is:



int array = { 11, 6, 87, 32, 15, 5, 9, 21 };


and the method is:



public int consecutivePairsSum_DivideAndConquer(int start, int end, int array) {
int leftSum;
int rightSum;
int middle = (start + end) / 2;
if (start == middle) {
return array[middle];
} else {
leftSum = array[start] + array[start + 1];
leftSum += consecutivePairsSum_DivideAndConquer(start, middle, array);
}
if (middle == end) {
return array[end];
} else {
rightSum = array[middle] + array[middle+1];
rightSum += consecutivePairsSum_DivideAndConquer(middle+1, end, array);
}
return leftSum + rightSum;
}


Here's my method call:



System.out.println(rF.consecutivePairsSum_DivideAndConquer(0, array.length-1, array));


I think it must be something to do with how I have split the array but no amount of experimenting is giving me the right output.



Expected output: 340



Actual output: 330



Any suggestions most welcome, this is driving me nuts! :p



ps Any useful links to where I can find a solid online tutorial/good book about recursion would also be great (if that's within the scope of SO seeing how it's not direct help with a programming issue)







java arrays recursion divide-and-conquer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 19:49









PumpkinBreath

678




678












  • If I remove a number like 5 from the array the sum goes up! Hmm.
    – Joakim Danielson
    Nov 9 at 22:21










  • Oh yeah just did it as well and it went up?! This recursion stuff is really complicated to grasp, especially with multiple branches
    – PumpkinBreath
    Nov 9 at 22:26










  • Sorry for the irrelevant question: Why would you calculate this sum using recursion? This problem doesn't seem to be a good fit for a divide and conquer approach, although it can be done.
    – 0605002
    Nov 9 at 22:43










  • It's a task I've been given as a way to practice divide and conquer algorithms. It seems that a lot of practice q's to understand these concepts aren't particularly practical, they're just there to help your understanding of the principle
    – PumpkinBreath
    Nov 9 at 22:49


















  • If I remove a number like 5 from the array the sum goes up! Hmm.
    – Joakim Danielson
    Nov 9 at 22:21










  • Oh yeah just did it as well and it went up?! This recursion stuff is really complicated to grasp, especially with multiple branches
    – PumpkinBreath
    Nov 9 at 22:26










  • Sorry for the irrelevant question: Why would you calculate this sum using recursion? This problem doesn't seem to be a good fit for a divide and conquer approach, although it can be done.
    – 0605002
    Nov 9 at 22:43










  • It's a task I've been given as a way to practice divide and conquer algorithms. It seems that a lot of practice q's to understand these concepts aren't particularly practical, they're just there to help your understanding of the principle
    – PumpkinBreath
    Nov 9 at 22:49
















If I remove a number like 5 from the array the sum goes up! Hmm.
– Joakim Danielson
Nov 9 at 22:21




If I remove a number like 5 from the array the sum goes up! Hmm.
– Joakim Danielson
Nov 9 at 22:21












Oh yeah just did it as well and it went up?! This recursion stuff is really complicated to grasp, especially with multiple branches
– PumpkinBreath
Nov 9 at 22:26




Oh yeah just did it as well and it went up?! This recursion stuff is really complicated to grasp, especially with multiple branches
– PumpkinBreath
Nov 9 at 22:26












Sorry for the irrelevant question: Why would you calculate this sum using recursion? This problem doesn't seem to be a good fit for a divide and conquer approach, although it can be done.
– 0605002
Nov 9 at 22:43




Sorry for the irrelevant question: Why would you calculate this sum using recursion? This problem doesn't seem to be a good fit for a divide and conquer approach, although it can be done.
– 0605002
Nov 9 at 22:43












It's a task I've been given as a way to practice divide and conquer algorithms. It seems that a lot of practice q's to understand these concepts aren't particularly practical, they're just there to help your understanding of the principle
– PumpkinBreath
Nov 9 at 22:49




It's a task I've been given as a way to practice divide and conquer algorithms. It seems that a lot of practice q's to understand these concepts aren't particularly practical, they're just there to help your understanding of the principle
– PumpkinBreath
Nov 9 at 22:49












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Here's an outline of the algorithm:



Base case: If your array has less than two elements, the result is 0 (because there are no pairs).



Otherwise: Divide the array into two halves, calculate the results for left and right halves, then the result for the whole array would be <result of left half> + <result of right half> + <last element of left half> + <first element of right half> (Because the only pair missing here is the pair at the location of the split).



In java, it would be something like this:



int consPairSum(int array, int left, int right) {
if(right <= left + 1) return 0;

int mid = (left + right) / 2;
int leftPairSum = consPairSum(array, left, mid);
int rightPairSum = consPairSum(array, mid, right);
return leftPairSum + rightPairSum + array[mid - 1] + array[mid];
}


It should be called as



consPairSum(array, 0, array.length);





share|improve this answer























  • This is pretty much exactly what I've been after. This follows the divide and conquer principle and is really clean and readable. many thanks
    – PumpkinBreath
    Nov 9 at 23:07


















up vote
1
down vote













who said divide and conquer needs to divide into equal chunks you just need to divide into self similar problem. almost 1 liner.



static private int doTheThing(int list){
if (list.length==2)
return list[0]+list[1];
return list[0]+list[1]+doTheThing(Arrays.copyOfRange(list,1,list.length));
}





share|improve this answer

















  • 2




    It's just recursion, divide and conquer is supposed to simplify the problem exponentially on each division, it can also be run in parallel.
    – 11thdimension
    Nov 9 at 23:04











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',
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%2f53232400%2fhaving-trouble-with-divide-and-conquer-algorithm-for-adding-consecutive-pairs-of%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








up vote
2
down vote



accepted










Here's an outline of the algorithm:



Base case: If your array has less than two elements, the result is 0 (because there are no pairs).



Otherwise: Divide the array into two halves, calculate the results for left and right halves, then the result for the whole array would be <result of left half> + <result of right half> + <last element of left half> + <first element of right half> (Because the only pair missing here is the pair at the location of the split).



In java, it would be something like this:



int consPairSum(int array, int left, int right) {
if(right <= left + 1) return 0;

int mid = (left + right) / 2;
int leftPairSum = consPairSum(array, left, mid);
int rightPairSum = consPairSum(array, mid, right);
return leftPairSum + rightPairSum + array[mid - 1] + array[mid];
}


It should be called as



consPairSum(array, 0, array.length);





share|improve this answer























  • This is pretty much exactly what I've been after. This follows the divide and conquer principle and is really clean and readable. many thanks
    – PumpkinBreath
    Nov 9 at 23:07















up vote
2
down vote



accepted










Here's an outline of the algorithm:



Base case: If your array has less than two elements, the result is 0 (because there are no pairs).



Otherwise: Divide the array into two halves, calculate the results for left and right halves, then the result for the whole array would be <result of left half> + <result of right half> + <last element of left half> + <first element of right half> (Because the only pair missing here is the pair at the location of the split).



In java, it would be something like this:



int consPairSum(int array, int left, int right) {
if(right <= left + 1) return 0;

int mid = (left + right) / 2;
int leftPairSum = consPairSum(array, left, mid);
int rightPairSum = consPairSum(array, mid, right);
return leftPairSum + rightPairSum + array[mid - 1] + array[mid];
}


It should be called as



consPairSum(array, 0, array.length);





share|improve this answer























  • This is pretty much exactly what I've been after. This follows the divide and conquer principle and is really clean and readable. many thanks
    – PumpkinBreath
    Nov 9 at 23:07













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Here's an outline of the algorithm:



Base case: If your array has less than two elements, the result is 0 (because there are no pairs).



Otherwise: Divide the array into two halves, calculate the results for left and right halves, then the result for the whole array would be <result of left half> + <result of right half> + <last element of left half> + <first element of right half> (Because the only pair missing here is the pair at the location of the split).



In java, it would be something like this:



int consPairSum(int array, int left, int right) {
if(right <= left + 1) return 0;

int mid = (left + right) / 2;
int leftPairSum = consPairSum(array, left, mid);
int rightPairSum = consPairSum(array, mid, right);
return leftPairSum + rightPairSum + array[mid - 1] + array[mid];
}


It should be called as



consPairSum(array, 0, array.length);





share|improve this answer














Here's an outline of the algorithm:



Base case: If your array has less than two elements, the result is 0 (because there are no pairs).



Otherwise: Divide the array into two halves, calculate the results for left and right halves, then the result for the whole array would be <result of left half> + <result of right half> + <last element of left half> + <first element of right half> (Because the only pair missing here is the pair at the location of the split).



In java, it would be something like this:



int consPairSum(int array, int left, int right) {
if(right <= left + 1) return 0;

int mid = (left + right) / 2;
int leftPairSum = consPairSum(array, left, mid);
int rightPairSum = consPairSum(array, mid, right);
return leftPairSum + rightPairSum + array[mid - 1] + array[mid];
}


It should be called as



consPairSum(array, 0, array.length);






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered Nov 9 at 22:52









0605002

10.2k32362




10.2k32362












  • This is pretty much exactly what I've been after. This follows the divide and conquer principle and is really clean and readable. many thanks
    – PumpkinBreath
    Nov 9 at 23:07


















  • This is pretty much exactly what I've been after. This follows the divide and conquer principle and is really clean and readable. many thanks
    – PumpkinBreath
    Nov 9 at 23:07
















This is pretty much exactly what I've been after. This follows the divide and conquer principle and is really clean and readable. many thanks
– PumpkinBreath
Nov 9 at 23:07




This is pretty much exactly what I've been after. This follows the divide and conquer principle and is really clean and readable. many thanks
– PumpkinBreath
Nov 9 at 23:07












up vote
1
down vote













who said divide and conquer needs to divide into equal chunks you just need to divide into self similar problem. almost 1 liner.



static private int doTheThing(int list){
if (list.length==2)
return list[0]+list[1];
return list[0]+list[1]+doTheThing(Arrays.copyOfRange(list,1,list.length));
}





share|improve this answer

















  • 2




    It's just recursion, divide and conquer is supposed to simplify the problem exponentially on each division, it can also be run in parallel.
    – 11thdimension
    Nov 9 at 23:04















up vote
1
down vote













who said divide and conquer needs to divide into equal chunks you just need to divide into self similar problem. almost 1 liner.



static private int doTheThing(int list){
if (list.length==2)
return list[0]+list[1];
return list[0]+list[1]+doTheThing(Arrays.copyOfRange(list,1,list.length));
}





share|improve this answer

















  • 2




    It's just recursion, divide and conquer is supposed to simplify the problem exponentially on each division, it can also be run in parallel.
    – 11thdimension
    Nov 9 at 23:04













up vote
1
down vote










up vote
1
down vote









who said divide and conquer needs to divide into equal chunks you just need to divide into self similar problem. almost 1 liner.



static private int doTheThing(int list){
if (list.length==2)
return list[0]+list[1];
return list[0]+list[1]+doTheThing(Arrays.copyOfRange(list,1,list.length));
}





share|improve this answer












who said divide and conquer needs to divide into equal chunks you just need to divide into self similar problem. almost 1 liner.



static private int doTheThing(int list){
if (list.length==2)
return list[0]+list[1];
return list[0]+list[1]+doTheThing(Arrays.copyOfRange(list,1,list.length));
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 22:55









mavriksc

50548




50548








  • 2




    It's just recursion, divide and conquer is supposed to simplify the problem exponentially on each division, it can also be run in parallel.
    – 11thdimension
    Nov 9 at 23:04














  • 2




    It's just recursion, divide and conquer is supposed to simplify the problem exponentially on each division, it can also be run in parallel.
    – 11thdimension
    Nov 9 at 23:04








2




2




It's just recursion, divide and conquer is supposed to simplify the problem exponentially on each division, it can also be run in parallel.
– 11thdimension
Nov 9 at 23:04




It's just recursion, divide and conquer is supposed to simplify the problem exponentially on each division, it can also be run in parallel.
– 11thdimension
Nov 9 at 23:04


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232400%2fhaving-trouble-with-divide-and-conquer-algorithm-for-adding-consecutive-pairs-of%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

Bressuire

Vorschmack

Quarantine