Created
August 14, 2017 15:43
-
-
Save sdpatil/fc90e643ce7679856b8610563c325bf5 to your computer and use it in GitHub Desktop.
Find the min and max simultaneously
This file contains hidden or 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
/** | |
* Problem is how do you find out minimum and maximum element in unsorted array without 2(n-1) comparison's | |
* Ex. {3,2,5,1,2,4} should return 1 min and 5 max | |
*/ | |
public class FindMinMaxSimul { | |
public static class MinMax { | |
public Integer min; | |
public Integer max; | |
public MinMax(Integer min, Integer max) { | |
this.min = min; | |
this.max = max; | |
} | |
} | |
/** | |
* Solution :- Basic idea is you take 2 elements at a time, first you find min and max between those 2 elements, | |
* then compare local min and max with global min and max | |
*/ | |
public MinMax findMinMax(int[] A) { | |
if(A.length <=1) | |
return new MinMax(A[0],A[1]); | |
int globalMin,globalMax; | |
globalMin = Math.min(A[0],A[1]); | |
globalMax = Math.max(A[0],A[1]); | |
for(int i = 2; i +1 < A.length; i= i+2){ | |
int localMin = Math.min(A[i],A[i+1]); | |
int localMax = Math.max(A[i],A[i+1]); | |
globalMin = Math.min(localMin,globalMin); | |
globalMax = Math.max(localMax,globalMax); | |
} | |
if( A.length%2 != 0){ | |
globalMin = Math.min(globalMin, A[A.length-1]); | |
globalMax = Math.max(globalMax, A[A.length-1]); | |
} | |
return new MinMax(globalMin,globalMax); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment