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










3














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















3














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













3












3








3


0





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

1019




1019











  • 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


















2














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


















1














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',
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%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









2














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















2














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













2












2








2






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 Nov 15 at 17:36

























answered Nov 9 at 22:52









0605002

10.2k32462




10.2k32462











  • 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













1














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















1














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













1












1








1






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

55548




55548







  • 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
















































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.




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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages