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)
java arrays recursion divide-and-conquer
add a comment |
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)
java arrays recursion divide-and-conquer
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
add a comment |
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)
java arrays recursion divide-and-conquer
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
java arrays recursion divide-and-conquer
asked Nov 9 at 19:49
PumpkinBreath
748
748
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
add a comment |
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
add a comment |
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);
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
add a comment |
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));
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
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
edited Nov 15 at 17:36
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
add a comment |
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
add a comment |
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));
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
add a comment |
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));
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
add a comment |
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));
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));
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
add a comment |
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
add a comment |
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%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
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
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