Skip to content

Instantly share code, notes, and snippets.

@vinodkd
Created March 11, 2012 18:52
Show Gist options
  • Save vinodkd/2017661 to your computer and use it in GitHub Desktop.
Save vinodkd/2017661 to your computer and use it in GitHub Desktop.

Revisions

  1. vinodkd revised this gist Mar 11, 2012. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,8 @@ 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++){
    //eg: 5,4,23,28,26
    //max1=-I, 5,5,23,28,28
    //max2=-I,-I,4, 5,23,26

    // recalc the new max3 numbers
    int val= input[i];
    if(val > max1){
    max2 = max1; // dont lose the old max1
    @@ -17,13 +16,15 @@ else if(val > max2 && val < max1){
    else if (val > max3 && val < max2) {
    max3 = val;
    }
    // end recalc max

    // |||ly the mins
    // recalc the min 2 numbers
    if(val < min1){
    min2 = min1;
    min1 = val;
    }
    else if(val < min2 && val > min1){
    min2 = val;
    }
    // end recalc min
    }
  2. vinodkd created this gist Mar 11, 2012.
    29 changes: 29 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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++){
    //eg: 5,4,23,28,26
    //max1=-I, 5,5,23,28,28
    //max2=-I,-I,4, 5,23,26
    int val= input[i];
    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;
    }

    // |||ly the mins
    if(val < min1){
    min2 = min1;
    min1 = val;
    }
    else if(val < min2 && val > min1){
    min2 = val;
    }
    }