Created
April 24, 2014 06:24
-
-
Save gabhi/11243583 to your computer and use it in GitHub Desktop.
find min and max in minimum comparisons
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
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