// Path: src/main/java/ro/hasna/ts/math/exception/ArrayLengthIsTooSmallException.java
// public class ArrayLengthIsTooSmallException extends NumberIsTooSmallException {
//     private static final long serialVersionUID = -2633584088507009304L;
// 
//     /**
//      * Construct the exception.
//      *
//      * @param wrong          Value that is smaller than the minimum.
//      * @param min            Minimum.
//      * @param boundIsAllowed Whether {@code min} is included in the allowed range.
//      */
//     public ArrayLengthIsTooSmallException(Number wrong, Number min, boolean boundIsAllowed) {
//         super(wrong, min, boundIsAllowed);
//     }
// 
//     /**
//      * Construct the exception with a specific context.
//      *
//      * @param specific       Specific context pattern.
//      * @param wrong          Value that is smaller than the minimum.
//      * @param min            Minimum.
//      * @param boundIsAllowed Whether {@code min} is included in the allowed range.
//      */
//     public ArrayLengthIsTooSmallException(Localizable specific, Number wrong, Number min, boolean boundIsAllowed) {
//         super(specific, wrong, min, boundIsAllowed);
//     }
// }
// 
// Path: src/main/java/ro/hasna/ts/math/type/MeanLastPair.java
// @Data
// public class MeanLastPair {
//     /**
//      * The mean value for the segment.
//      */
//     private final double mean;
//     /**
//      * The position from the last element of the segment.
//      */
//     private final int last;
// }
// 
// Path: src/main/java/ro/hasna/ts/math/util/TimeSeriesPrecision.java
// public class TimeSeriesPrecision {
//     public static final double EPSILON = FastMath.pow(2, -30);
// 
//     /**
//      * Private constructor.
//      */
//     private TimeSeriesPrecision() {
//     }
// }


import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.Precision;
import ro.hasna.ts.math.exception.ArrayLengthIsTooSmallException;
import ro.hasna.ts.math.type.MeanLastPair;
import ro.hasna.ts.math.util.TimeSeriesPrecision;
import java.util.Set;
import java.util.TreeSet;

                    set.add(minSegment.prev);
                }

                numberOfSegments--;
            }
        }

        return getMeanLastPairs(first, numberOfSegments);
    }

    private Segment createSegments(double[] values, int length) {
        Segment first = null, last = null;
        for (int i = 0; i < length - 1; i += 2) {
            double mean = (values[i] + values[i + 1]) / 2;
            Segment segment = new Segment(i, i + 2, mean, 2 * FastMath.abs(values[i] - mean));
            if (first == null) {
                first = segment;
                last = first;
            } else {
                last.next = segment;
                segment.prev = last;
                last = last.next;
            }
        }
        return first;
    }

    private TreeSet<Segment> createSegmentsSet(double[] values, Segment first) {
        TreeSet<Segment> map = new TreeSet<>((s1, s2) ->
// Path: src/main/java/ro/hasna/ts/math/exception/ArrayLengthIsTooSmallException.java
// public class ArrayLengthIsTooSmallException extends NumberIsTooSmallException {
//     private static final long serialVersionUID = -2633584088507009304L;
// 
//     /**
//      * Construct the exception.
//      *
//      * @param wrong          Value that is smaller than the minimum.
//      * @param min            Minimum.
//      * @param boundIsAllowed Whether {@code min} is included in the allowed range.
//      */
//     public ArrayLengthIsTooSmallException(Number wrong, Number min, boolean boundIsAllowed) {
//         super(wrong, min, boundIsAllowed);
//     }
// 
//     /**
//      * Construct the exception with a specific context.
//      *
//      * @param specific       Specific context pattern.
//      * @param wrong          Value that is smaller than the minimum.
//      * @param min            Minimum.
//      * @param boundIsAllowed Whether {@code min} is included in the allowed range.
//      */
//     public ArrayLengthIsTooSmallException(Localizable specific, Number wrong, Number min, boolean boundIsAllowed) {
//         super(specific, wrong, min, boundIsAllowed);
//     }
// }
// 
// Path: src/main/java/ro/hasna/ts/math/type/MeanLastPair.java
// @Data
// public class MeanLastPair {
//     /**
//      * The mean value for the segment.
//      */
//     private final double mean;
//     /**
//      * The position from the last element of the segment.
//      */
//     private final int last;
// }
// 
// Path: src/main/java/ro/hasna/ts/math/util/TimeSeriesPrecision.java
// public class TimeSeriesPrecision {
//     public static final double EPSILON = FastMath.pow(2, -30);
// 
//     /**
//      * Private constructor.
//      */
//     private TimeSeriesPrecision() {
//     }
// }


// Path: src/main/java/ro/hasna/ts/math/representation/AdaptivePiecewiseConstantApproximation.java
import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.Precision;
import ro.hasna.ts.math.exception.ArrayLengthIsTooSmallException;
import ro.hasna.ts.math.type.MeanLastPair;
import ro.hasna.ts.math.util.TimeSeriesPrecision;
import java.util.Set;
import java.util.TreeSet;


                    set.add(minSegment.prev);
                }

                numberOfSegments--;
            }
        }

        return getMeanLastPairs(first, numberOfSegments);
    }

    private Segment createSegments(double[] values, int length) {
        Segment first = null, last = null;
        for (int i = 0; i < length - 1; i += 2) {
            double mean = (values[i] + values[i + 1]) / 2;
            Segment segment = new Segment(i, i + 2, mean, 2 * FastMath.abs(values[i] - mean));
            if (first == null) {
                first = segment;
                last = first;
            } else {
                last.next = segment;
                segment.prev = last;
                last = last.next;
            }
        }
        return first;
    }

    private TreeSet<Segment> createSegmentsSet(double[] values, Segment first) {
        TreeSet<Segment> map = new TreeSet<>((s1, s2) ->
                Precision.compareTo(s1.errorWithNext, s2.errorWithNext, TimeSeriesPrecision.EPSILON));