package de.sebhn.algorithm.excercise2;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class MinProduct {


  public static void main(String[] args) throws Exception {
    double[] someRandomArray = someOtherRandomArray();
    System.out.println("Array: " + Arrays.toString(someRandomArray));
    if (someRandomArray.length == 0) {
      throw new Exception("Please fill array");
    }
    double firstEntry = someRandomArray[0];
    double current_min_prod = firstEntry;
    double current_max_prod = firstEntry;
    double previous_min_prod = firstEntry;
    double previous_max_prod = firstEntry;
    double result = firstEntry;
    int indexFrom = 0;
    for (int i = 1; i < someRandomArray.length; i++) {
      double myDouble = someRandomArray[i];
      // System.out.println("calc min " + previous_min_prod + "*" + myDouble);
      // System.out.println("calc max " + previous_max_prod + "*" + myDouble);
      double temporaryMax = previous_max_prod * myDouble;
      double temporaryMin = previous_min_prod * myDouble;
      current_max_prod = Math.max(Math.max(temporaryMax, temporaryMin), myDouble);
      current_min_prod = Math.min(Math.min(temporaryMax, temporaryMin), myDouble);
      System.out.println("new min " + current_min_prod + " new max " + current_max_prod);

      // wenn current max < temporarMax, currentMax index = tempIndex

      // momentane max < momentan min = vorzeichenwechsel //wechsel von min/max
      if (current_min_prod > 1) {
        current_min_prod = 1;
        indexFrom = i;
        System.out.println("reseted");
        // neue teilfolge
      }

      result = Math.min(result, current_min_prod);
      previous_max_prod = current_max_prod;
      previous_min_prod = current_min_prod;
      // System.out.println(result);
    }

    String factors = IntStream.range(indexFrom, someRandomArray.length) // start from indexFrom
        .mapToObj(i -> String.valueOf(Double.valueOf(someRandomArray[i]))) // assign value to String
        .collect(Collectors.joining("*", " calculated from ", ""));
    System.out.println("result: " + result + factors);
  }

  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;
  }

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

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

}

@@ -157,16 +157,169 @@
 duct %7B%0A%0A
+  private static int indexStart = 0;%0A  private static int indexEnd = 0;%0A  private static int indexStartScan = 0;%0A  private static int indexEndScan = 0;%0A%0A
 %0A  publi
@@ -957,16 +957,40 @@
 ray%5Bi%5D;%0A
+      indexEndScan = i;%0A
       //
@@ -1746,24 +1746,78 @@
 exFrom = i;%0A
+        indexStartScan = i;%0A        indexEnd = i - 1;%0A
         Syst
@@ -1929,16 +1929,134 @@
 _prod);%0A
+      if (current_min_prod %3C= result) %7B%0A        indexStart = indexStartScan;%0A        indexEnd = indexEndScan;%0A      %7D%0A
       pr
@@ -2172,16 +2172,87 @@
 esult);%0A
+      System.out.println(%22Index ist: %22 + indexStart + %22 %22 + indexEnd);%0A
     %7D%0A%0A 
@@ -3424,16 +3424,18 @@
 ay%5B0%5D = 
+0.
 2;%0A    r
@@ -3787,25 +3787,25 @@
 Array%5B0%5D = -
-3
+2
 ;%0A    random
@@ -3807,33 +3807,40 @@
 andomArray%5B1%5D = 
-3
+-3000000
 ;%0A    randomArra
@@ -3842,25 +3842,28 @@
 mArray%5B2%5D = 
-3
+-300
 ;%0A    random
@@ -3917,25 +3917,30 @@
 mArray%5B5%5D = 
-3
+-30000
 ;%0A    random
@@ -4018,26 +4018,27 @@
 mArray%5B9%5D = 
--4
+100
 ;%0A    return

add end index