Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created April 24, 2014 06:24
Show Gist options
  • Save gabhi/11243583 to your computer and use it in GitHub Desktop.
Save gabhi/11243583 to your computer and use it in GitHub Desktop.
find min and max in minimum comparisons
class Pair {
int min;
int max;
}
public class Solution {
public static Pair getMinMax(int arr[], int low, int high) {
Pair result = new Pair();
Pair left = new Pair();
Pair right = new Pair();
// if there is only one element
if (low == high) {
result.max = arr[low];
result.min = arr[low];
return result;
}
// if there are two elements
if (high == low + 1) {
if (arr[low] > arr[high]) {
result.max = arr[low];
result.min = arr[high];
} else {
result.max = arr[high];
result.min = arr[low];
}
return result;
}
// if there are more than 2 elements
int mid = (low + high) / 2;
left = getMinMax(arr, low, mid);
right = getMinMax(arr, mid + 1, high);
if (left.min < right.min)
result.min = left.min;
else
result.min = right.min;
if (left.max > right.max)
result.max = left.max;
else
result.max = right.max;
return result;
}
public static void main(String[] args) {
int a1[] = { 3, 4, 2, 6, 8, 1, 9, 12, 15, 11 };
Pair result = getMinMax(a1, 0, a1.length - 1);
System.out.println("Min: " + result.min);
System.out.println("Max: " + result.max);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment