Skip to content

Instantly share code, notes, and snippets.

@sax1johno
Last active August 29, 2015 14:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sax1johno/ed239bcc8e556195a928 to your computer and use it in GitHub Desktop.
/* 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