package de.sebhn.algorithm.excercise2;

import static java.lang.Math.min;

import java.util.Arrays;

public class MinProduct {


  public static void main(String[] args) {
    double scanMin = 1;
    double bisMin = 1;
    double[] someRandomArray = someRandomArray();
    for (int i = 0; i < someRandomArray.length; i++) {
      double myDouble = someRandomArray[i];
      System.out.println("calc" + scanMin + "*" + myDouble);
      scanMin *= myDouble;
      if (scanMin > 1) {
        if (!hasNegativeNumberInRemainingArray(someRandomArray, i)) {
          scanMin /= myDouble;
        }
      }
      System.out.println("bisMin" + bisMin + " scanMin:" + scanMin);
      bisMin = min(scanMin, bisMin);
    }
    System.out.println("Array: " + Arrays.toString(someRandomArray));
  }

  private static boolean hasNegativeNumberInRemainingArray(double[] arrayToCheck,
      int currentPosition) {
    for (int j = currentPosition; j < arrayToCheck.length; j++) {
      double aPossibleNegative = arrayToCheck[currentPosition];
      if (aPossibleNegative < 0) {
        return true;
      }
    }
    return false;
  }

  public static double[] someRandomArray() {
    double[] randomArray = new double[10];
    randomArray[0] = 1;
    randomArray[1] = -13;
    randomArray[2] = 4.5;
    randomArray[3] = 7;
    randomArray[4] = 98;
    randomArray[5] = -33.5;
    randomArray[6] = 3;
    randomArray[7] = 64;
    randomArray[8] = 8;
    randomArray[9] = -2;
    return randomArray;
  }

  public static double[] someNegativeArray() {
    double[] randomArray = new double[10];
    randomArray[0] = -1;
    randomArray[1] = -2;
    randomArray[2] = -3;
    randomArray[3] = -4;
    randomArray[4] = -5;
    randomArray[5] = -6;
    randomArray[6] = -7;
    randomArray[7] = -8;
    randomArray[8] = -9;
    randomArray[9] = -10;
    return randomArray;
  }
}

@@ -39,45 +39,8 @@
 %0D%0A%0D%0A
-import static java.lang.Math.min;%0D%0A%0D%0A
 impo
@@ -139,58 +139,26 @@
 gs) 
-%7B%0D%0A    double scanMin = 1;%0D%0A    double bisMin = 1;
+throws Exception %7B
 %0D%0A  
@@ -214,28 +214,83 @@
     
-for (int i = 0; i %3C 
+System.out.println(%22Array: %22 + Arrays.toString(someRandomArray));%0D%0A    if (
 some
@@ -311,13 +311,13 @@
 ngth
-; i++
+ == 0
 ) %7B%0D
@@ -327,23 +327,79 @@
     
-double myDouble
+throw new Exception(%22Please fill array%22);%0D%0A    %7D%0D%0A    double firstEntry
  = s
@@ -417,17 +417,17 @@
 ray%5B
-i
+0
 %5D;%0D%0A    
   Sy
@@ -426,248 +426,320 @@
     
-  System.out.println(%22calc%22 + scanMin + %22*%22 + myDouble);%0D%0A      scanMin *= myDouble;%0D%0A      if (scanMin %3E 1) %7B%0D%0A        if (!hasNegativeNumberInRemainingArray(someRandomArray, i)) %7B%0D%0A          scanMin /= myDouble;%0D%0A        %7D%0D%0A      %7D%0D%0A     
+double current_min_prod = firstEntry;%0D%0A    double current_max_prod = firstEntry;%0D%0A    double previous_min_prod = firstEntry;%0D%0A    double previous_max_prod = firstEntry;%0D%0A    double result = firstEntry;%0D%0A    for (int i = 1; i %3C someRandomArray.length; i++) %7B%0D%0A      double myDouble = someRandomArray%5Bi%5D;%0D%0A      //
  Sys
@@ -759,94 +759,63 @@
 ln(%22
-bisM
+calc m
 in
+ 
 %22 + 
-bisMin + %22 scanMin:%22 + scanMin);%0D%0A      bisMin = min(scanMin, bisMin
+previous_min_prod + %22*%22 + myDouble
 );%0D%0A    
 %7D%0D%0A 
@@ -810,22 +810,20 @@
 );%0D%0A    
-%7D%0D%0A 
   
+//
  System.
@@ -839,151 +839,183 @@
 ln(%22
-Array: %22 + Arrays.toString(someRandomArray));%0D%0A  %7D%0D%0A%0D%0A  private static boolean hasNegativeNumberInRemainingArray(double%5B%5D arrayToCheck,
+calc max %22 + previous_max_prod + %22*%22 + myDouble);%0D%0A      double temporaryMax = previous_max_prod * myDouble;%0D%0A      double temporaryMin = previous_min_prod * myDouble;
 %0D%0A      
 int 
@@ -1014,254 +1014,422 @@
     
-int 
 current
-Position) %7B%0D%0A    for (int j = currentPosition; j %3C arrayToCheck.length; j++) %7B%0D%0A      double aPossibleNegative = arrayToCheck%5BcurrentPosition%5D;%0D%0A      if (aPossibleNegative %3C 0) %7B%0D%0A        return true;%0D%0A      %7D%0D%0A    %7D%0D%0A    return false
+_max_prod = Math.max(Math.max(temporaryMax, temporaryMin), myDouble);%0D%0A      current_min_prod = Math.min(Math.min(temporaryMax, temporaryMin), myDouble);%0D%0A%0D%0A      result = Math.min(Math.min(result, current_min_prod), myDouble);%0D%0A      previous_max_prod = current_max_prod;%0D%0A      previous_min_prod = current_min_prod;%0D%0A      // System.out.println(result);%0D%0A    %7D%0D%0A    System.out.println(%22result: %22 + result)
 ;%0D%0A 

solve problem :)