Lets assume a is 1, and b is 2.
if (a <= 0 && b <= 0) result = getSum(a + 1, b + 1) + a + b;
if (a >= 0 && b >= 0) result = getSum(a - 1, b - 1) + a + b;
The 2nd kicks in:
result = getSum(1 - 1, 2 - 1) + a + b;
So you call: with a = 0, b = 2. That one picks:
result = getSum(0 + 1, 1 + 1) + a + b;
So you are back to call with 1, 2.
And it starts from scratch. Leading into an endless recursion.
So, there are multiple problems with your current approach:
- both if conditions might apply. As you use "<=" and ">=", when a or b are 0, both if conditions kick in
- worse: as shown, your setup allows to easily go down-up-down-up-... forever
- beyond that: a correct "stop recursion" is missing. For certain input, your code will simply always go and call itself, again.
- guessing here: you are also missing corner cases, such as a < 0, but b > 0
So, long story short: your whole algorithm is bogus, and you need to step back and rethink what you are doing here!