I'm trying to solve this problem using recursion, but I seem to run into a stack overflow error beyond a certain divided that's not >= Integer.MAX_VALUE. Could anyone provide some insight into this issue?
class Solution {
int count = 0;
public int divide(int dividend, int divisor) {
int temp1 = Math.abs(dividend);
int temp2 = Math.abs(divisor);
if(dividend > Integer.MAX_VALUE || dividend < Integer.MIN_VALUE){
if ((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0)){
System.out.println("executed");
return Integer.MAX_VALUE;
}else{
return Integer.MIN_VALUE;
}
}
divideHelper(temp1, temp2);
if (dividend < 0 && divisor < 0){
return count-1;
}
if (dividend < 0 || divisor < 0){
return -(count - 1);
}
return count-1;
}
public int divideHelper (int dividend1, int divisor1){
if (dividend1 < 0) {
return dividend1;
}
if (dividend1 >= 0) {
dividend1 -= divisor1;
count++;
}
divideHelper(dividend1, divisor1);
return count;
}
}