public static int greatMultiply3(int[] arr){
         //find top 3 maximum and bottom two minimum in linear scan. return greater of two

         int max=Integer.MIN_VALUE, secondmax=Integer.MIN_VALUE,
                 thirdmax=Integer.MIN_VALUE,
                 min = Integer.MAX_VALUE, secondmin = Integer.MAX_VALUE;

         //assuming at least three elements in array

         for(int i = 0;i < arr.length;i++) {
             int element = arr[i];
             if(element>max) {
                 max = element;
                 secondmax =max;
                 thirdmax =secondmax;
             } else if (element > secondmax && element <= max) {
                 secondmax = element;
                 thirdmax = secondmax;
             } else if(element > thirdmax && element <= secondmax){
                 thirdmax = element;
             } else if (element < min) {
                 min = element;
                 secondmin = min;
             } else {
                 if(element < secondmin && element >=min){
                     secondmin = element;
                 }
             }

         }

         return  Math.max(max*secondmax*thirdmax,max*min*secondmin);
     }