I have an array of numbers should return true if 2 adjacent numbers divide by 10.
For now, my code return always false
.
My attempt:
public static boolean divideByTen(int arr[], int num) {
int i = num - 1;
if (i > 0) {
divideByTen(arr, num - 1);
if (arr[i] arr[i - 1] % 10 == 0)
return true;
}
return false;
}
CodePudding user response:
divideByTen(arr, num - 1);
This invokes divideByTen
(recurses) and throws away the result. Recursion isn't magic or special; you're just.. calling a method. It happens to be the same method you're in, but that's all. Calling a method without assigning its value to anything means the returned value is discarded.
Presumably, you wanted if (divideByTen(arr, num -1)) return true;
instead.
CodePudding user response:
I tried to store the boolean value in one variable. I think in your case it will return false unless your if condition is not true at the end.
public static void main (String[] args)
{
int[] a = new int[] {1, 2, 3, 4, 1, 2, 30, 2};
System.out.println(divideByTen(a, a.length - 1));
}
public static boolean divideByTen(int arr[], int num){
boolean flag = false;
if(num < 1){
return flag;
}
flag = divideByTen(arr, num -1);
if(num > 0){
if((arr[num-1] arr[num]) % 10 == 0){
flag = true;
}
}
return flag;
}
CodePudding user response:
How to approach Recursion
In order to implement a recursive method you should always think what would be the Base case for this problem and should happen in the Recursive case.
Base case - is an edge-case (or a group of edge-cases) when recursion should terminate, and return value is known is advance, because base case always trivial situations. The Base case for this problem is when there's no more elements.
Recursive case - is where logic of the computations resides, and where recursive calls are made. In the recursive case you need to evaluate the current pair of elements and if they match the condition return the result, otherwise proceed with a recursive call. For that, you can make use of the
||
operator, known as Conditional-OR.
Note that both Base case and Recursive case are always present in any recursive implementation (either explicitly or implicitly), and firm understanding of these two notions would make your life easier.
Implementation
It seems like some users interpreted the problem statement in the wrong way as a Sum of two adjacent elements is divisible by 10 instead of Two adjacent elements are divisible by 10 because of the mistake in your code, although the sum is not mentioned in the question. But this doesn't make any difference to the algorithm, and I'll show both implementation.
Two adjacent elements are divisible by 10:
public static boolean hasAdjacentDivisibleByTen(int[] arr) {
if (arr.length < 2) return false; // or throw an exception because there's no two adjacent elements in the given list
return hasAdjacentDivisibleByTen(arr, arr.length - 1);
}
public static boolean hasAdjacentDivisibleByTen(int[] arr, int pos) {
if (pos == 0) return false; // Base case - no more elements to check left
// Recursive case - return current result or proceed with next call (if result is false)
return arr[pos] % 10 == 0 && arr[pos - 1] % 10 == 0 || hasAdjacentDivisibleByTen(arr, pos - 1);
}
Sum of two adjacent elements is divisible by 10:
public static boolean hasAdjacentSumDivisibleByTen(int[] arr) {
if (arr.length < 2) return false;
return hasAdjacentSumDivisibleByTen(arr, arr.length - 1);
}
public static boolean hasAdjacentSumDivisibleByTen(int[] arr, int pos) {
if (pos == 0) return false; // Base case - no more elements to check left
// Recursive case - return current result or proceed with next call (if result is false)
return (arr[pos] arr[pos - 1]) % 10 == 0 || hasAdjacentDivisibleByTen(arr, pos - 1);
}