Reputation: 11
The following error shows: at parenthesis.check.printcombination(check.java:51)
at parenthesis.check.printcombination(check.java:58) My 51st and 56th line are commented below in the code
package parenthesis;
import java.util.*;
public class check {
public static void main(String args[]) {
int arr[] = { 12, 13, 14, 15, 16, 17, 18, 19 };
function(arr, 0, 7);
}
public static void function(int arr[], int start, int end) {
int mid;
mid = (start + end) / 2;
printcombination(arr, start, end, mid);
}
public static void printcombination(int arr[], int start, int end, int mid) {
System.out.print(arr[mid] + " "); //// error
if ((start == mid) && (mid == end)) {
return;
}
printcombination(arr, start, mid - 1, (start + mid - 1) / 2); //// error
printcombination(arr, mid + 1, end, (mid + 1 + end) / 2);
}
}
Upvotes: 0
Views: 89
Reputation: 11
There is a bad recursion at line 34 ("printcombination(arr,start,mid-1,(start+mid-1)/2); ////error"), StackOverflowError error is thrown here. Recursion flow for "printcombination" method is:
start = 0, mid = 3, end = 7, call "printcombination(arr,start,mid-1,(start+mid-1)/2)";
start = 0, mid = 1, end = 2, call "printcombination(arr,start,mid-1,(start+mid-1)/2)";
start = 0, mid = 0, end = 0, return;
start = 0, mid = 1, end = 2, call "printcombination(arr,mid+1,end,(mid+1+end)/2);
start = 0, mid = 3, end = 7, call printcombination(arr,mid+1,end,(mid+1+end)/2);
start = 4, mid = 5, end = 7, call "printcombination(arr,start,mid-1,(start+mid-1)/2)";
start = 4, mid = 4, end = 4, return;
start = 4, mid = 5, end = 7, call "printcombination(arr,mid+1,end,(mid+1+end)/2)";
start = 6, mid = 6, end = 7, call "printcombination(arr,start,mid-1,(start+mid-1)/2)";
start = 6, mid = 5, end = 5, call "printcombination(arr,start,mid-1,(start+mid-1)/2)";
start = 6, mid = 5, end = 4, call "printcombination(arr,start,mid-1,(start+mid-1)/2)";
start = 6, mid = 5, end = 4, call "printcombination(arr,start,mid-1,(start+mid-1)/2)";
...
Upvotes: 1