1

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?

  • 1
    Shouldn't the correct answer be 13? (11 - (-2)) – azurefrog Jan 02 '18 at 20:50
  • oops, yes. sory, im a wake to much time without sleeping. – Yacov cohen Jan 02 '18 at 20:55
  • This code doesn't return 7 for me; it returns 11 (the max value) -- basically because nowhere in here do you do anything except find `Math.max`. Maybe you need to do something like that again, this time to find the lowest number, then subtract the two results. – Gus Jan 02 '18 at 21:24

3 Answers3

1

Essentially when recursing you want to have changing values and have it return the final results when a specific criteria is met

I modified your code so that you pass in the array, followed by the initial index and set the min and max value to the first value in the array. It will recurse down and check if the next value in the array is greater than or less than the min and max and set accordingly. It will stop once the index is equal to the length of the array and return the final results:

 public static void main(String[] args) {
    int arr[] = {1 , 5 , 11, -2};
    System.out.println(pow(arr, 0, arr[0], arr[0])); 
  }

  public static int pow(int[] arr, int index, int min, int max) {
    if (index == arr.length) {
      return max - min;
    }
    int val = arr[index];
    int newMin = val < min ? val : min;
    int newMax = val > max ? val : max;
    return pow(arr, index + 1, newMin, newMax);
  }

Another way to do it based off Taras Sheremeta suggestion is something as follows:

public static void main(String[] args) {
    int arr[] = {1 , 5 , 11, -2};
    System.out.println(largest(arr, 0) - smallest(arr, 0)); 
  }

  public static int smallest(int[] arr, int index) {
    if (index == arr.length - 1) {
      return arr[index];
    }
    return Math.min(arr[index], smallest(arr, index + 1));
  }

  public static int largest(int[] arr, int index) {
    if (index == arr.length - 1) {
      return arr[index];
    }
    return Math.max(arr[index], largest(arr, index + 1));
  }

the functions will find their respective largest and smallest values recursively.

  • hi issaac, first thank for your time to help me. can you explain this line? val < min ? val : min; first time that i saw this, it is some short if statement? – Yacov cohen Jan 02 '18 at 21:51
  • Sure. Its an inline if statement called a ternary operatory. What it does it evaluates the condition before the `?` if the condition evaluates to true it will apply the condition between the `?` and the `:`. If the condition evaluates to false it will apply the condition after the `:`. Its kind of a quick way to do an if else for something simple. so for instance for the code `val < min ? val : min` it will test to see if val is less than min. if it is it will use val otherwise itll use min for newMin – Isaac Parenteau Jan 03 '18 at 13:43
0

Looks like the is some logical error in recursion. In the pow method functions Math.max(...) and Math.min(...) get a value from the array as the first argument and NOT a value from an array as the second argument. The result of pow function IS NOT a value from the array.

Taras Sheremeta
  • 117
  • 1
  • 1
  • 8
  • so what you suggest to do? , you can give me a direction? i only know how to get to the biggest/smallest value of array, but not sum the biggest value minus lowest value. – Yacov cohen Jan 02 '18 at 21:14
  • I suggest creating two separate functions for getting min and max value in the array. And after that just calculate `max-min`. The count of iteration will be the same and code will be more readable. – Taras Sheremeta Jan 02 '18 at 21:21
0
public static void main(String[] args) {
    int arr[] = {1 , 5 , 11, -2};
    System.out.println(pow(arr, 0, arr[0], arr[0]));
}
private static int pow(int[] arr, int index, int max, int min) {
    if (index == arr.length)  {
        return max - min;
    }

    max = Math.max(max, arr[index]);
    min = Math.min(min, arr[index]);
    return pow(arr, index + 1, max, min);
}

You can read more about How should you approach recursion?

Daniel Taub
  • 5,133
  • 7
  • 42
  • 72