Created
March 11, 2012 19:00
-
-
Save vinodkd/2017685 to your computer and use it in GitHub Desktop.
MaxProd.jak
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
| public static int maxProd(int[] input){ | |
| int max1 = max2 = max3 = java.lang.Integer.MIN_VALUE; | |
| int min1 = min2 java.lang.Integer.MAX_VALUE; | |
| for(int i=0; i<input.length;i++){ | |
| int val= input[i]; | |
| range calcmax{ | |
| if(val > max1){ | |
| max2 = max1; // dont lose the old max1 | |
| max1=val; | |
| } | |
| else if(val > max2 && val < max1){ | |
| max3 = max2; // dont lose the old max2 | |
| max2 = val; | |
| } | |
| else if (val > max3 && val < max2) { | |
| max3 = val; | |
| } | |
| }// end recalc max | |
| range calcMin{ | |
| if(val < min1){ | |
| min2 = min1; | |
| min1 = val; | |
| } | |
| else if(val < min2 && val > min1){ | |
| min2 = val; | |
| } | |
| }// end recalc min | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment