I try to write recursive program that return the biggest value - smallest value from array.
So I write this: (this return me the biggest value)
public static void main(String[] args) {
int arr[] = {1 , 5 , 11, -2};
System.out.println(SumOfBiggestMinusLowestValue(arr, 0));
}
private static int SumOfBiggestMinusLowestValue(int[] arr, int index) {
if (index == arr.length-1 ) {
return arr[index];
}
return Math.max (arr[index] ,SumOfBiggestMinusLowestValue(arr, index+1));
}
I though to do this to return big-min:
return Math.max (arr[index] ,SumOfBiggestMinusLowestValue(arr, index+1)) - Math.min(arr[index] ,SumOfBiggestMinusLowestValue(arr, index+1))
but it's not work its giving me 7 instead 13, what I missing? and from yours experience guys,how to think recursively?