CC-6 : Find max and min number.
Description:
Given an input array of numbers, find the maximum and minimum numbers.
Test cases and expected outputs:
| Input Parameters |
Expected outputs |
| Original Array: : 1,2,3,4,5,6,7,1
|
Min element of array: 1
Max element of array: 7
|
Pseudocode:
findMin(num):
| The array arr to be checked is received as input parameter.
|
| If num==1, return arr[0]; (base case).
|
| Recursively call findMin() with parameter arr[num-1] and then find the lesser value from return value from recursive call and num-1. Return the lesser value found from the current recursive method call.
|
findMax(num):
| The array arr to be checked is received as input parameter.
|
| If num==1, return arr[0]; (base case).
|
| Recursively call findMax() with parameter arr[num-1] and then find the greater value from return value from recursive call and num-1. Return the greater value found from the current recursive method call.
|
Code:
public int findMin(int[] arr, int n) throws Exception{
if (n==1) {
return arr[0];
}
return Math.min(arr[n-1], findMin(arr,n-1));
}
public int findMax(int[] arr, int n) throws Exception{
if (n==1) {
return arr[0];
}
return Math.max(arr[n-1], findMax(arr,n-1));
}
Click here to download and run code and test cases !
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class RecursionFindMaxAndMin {
public int findMin(int[] arr, int n) throws Exception{
if (n==1) {
return arr[0];
}
return Math.min(arr[n-1], findMin(arr,n-1));
}
public int findMax(int[] arr, int n) throws Exception{
if (n==1) {
return arr[0];
}
return Math.max(arr[n-1], findMax(arr,n-1));
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex < intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex < intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println(" ************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
public static void main(String[] args) {
/***********************
Test cases given below:
**********************/
int retVal;
try {
RecursionFindMaxAndMin ap=new RecursionFindMaxAndMin();
int[] intArray1 = {1,2,3,4,5,6,7,1};
printArraySummary(intArray1, "Original Array:");
retVal=ap.findMin(intArray1, 8);
System.out.println("Min element of array: "+retVal);
retVal=ap.findMax(intArray1, 8);
System.out.println("Max element of array: "+retVal);
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
}
}
}