This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* package whatever; // don't place package name! */ | |
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
/* Name of the class has to be "Main" only if the class is public. */ | |
class Recurse | |
{ | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
// your code goes here | |
getPeople(person1, person2, person3); | |
} | |
public int[] findMinMax(ArrayList<Integer) numbers) { | |
int min = numbers[0]; | |
int max = numbers[0]; | |
if (numbers[0] == 1) { | |
return { min, max } | |
} else { | |
for (int i = 0; i < numbers.size(); i++) { | |
if (numbers[i] < min) { | |
min = numbers[i]; | |
} | |
if (numbers[i] > max) { | |
max = numbers[i]; | |
} | |
} | |
return { min, max } | |
} | |
} | |
public int findMaxRecursively(ArrayList<Integer> numbers) { | |
// Terminal Condition (end of the recursion); | |
if (numbers.size() == 1) { | |
return numbers[0]; | |
} | |
// otherwise, let's find the max. | |
if (numbers[0] < numbers[1]) { | |
numbers.remove(0); | |
} else { | |
numbers.remove(1); | |
} | |
return findMaxRecursively(numbers); | |
} | |
} | |
/** | |
* return findMax(1, 2, 3, 4, 5) { | |
return findMax(2, 3, 4, 5) { | |
return findMax(3, 4, 5) { | |
return findMax(4, 5) { | |
return 5 | |
} | |
} | |
} | |
} | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment