title: I can't think of a condition to increase the decrement in Gradually Decreasing Carousel app
body: <p><code>Decrementing Carousel</code> is a container, accepting int elements. <code>DecrementingCarousel</code> has a maximum capacity, specified via the constructor. When created, DecrementingCarousel is in accumulating state: you may add elements via the <code>addElement</code> method and can produce a <code>CarouselRun</code> object via the <code>run</code> method. Once the <code>run</code> method is called, <code>DecrementingCarousel</code> is in running state: it refuses adding more elements.</p>
  <p>The <code>CarouselRun</code> allows to iterate over elements of the carousel decrementing them one by one with the <code>next</code> method. The <code>next</code> returns the value of the current element.</p>
  <p>The <code>CarouselRun</code> iterates over elements in the order of their insertion.
  When an element is decreased to zero, the <code>CarouselRun</code> will skip it in further iterations. When there are no more elements available for decrementing, the <code>CarouselRun</code> returns -1.</p>
  <p>The <code>CarouselRun</code> also has the <code>isFinished</code> method, which indicates, if the carousel has run out of the lements to decrement.</p>
  <p><em><strong>Specification Details</strong></em></p>
  <p><code>boolean addElement(int element)</code> - adds an element. If element is negative or zero, do not add the element. If container is full, do not add the element. If the run method was called to create a <code>CarouselRun</code>, do not add the element. If element is added successfully, return <code>true</code>. Return <code>false</code> otherwise.
  <code>CarouselRun run()</code> - returns a CarouselRun to iterate over the elements. If the run method has already been called earlier, it must return <code>null</code>: <code>DecrementingCarousel</code> may generate only one <code>CarouselRun</code> object.</p>
  <p><em><code>CarouselRun</code> has two public methods:</em></p>
  <p><code>int next()</code> - returns the current value of the current element, then decreases the current element by one and switches to the next element in insertion order. Skips zero elements. When there is no more elements to decrease, returns -1.
  <code>boolean isFinished()</code> - when there is no more elements to decrease, returns <code>true</code>. Otherwise, returns <code>false</code>.
  <strong>The code:</strong></p>
  <pre><code>public class DecrementingCarousel {
      private final int capacity;
      static int[] carousel;
      int index;
      boolean isRun;
  
      {
          index = 0;
          isRun = false;
      }
  
      public DecrementingCarousel(int capacity) {
          this.capacity = capacity;
          carousel = new int[capacity];
      }
  
      public boolean addElement(int element){
          if (element &gt; 0 &amp;&amp; index &lt; capacity &amp;&amp; !isRun) {
              carousel[index++] = element;
              return true;
          }
          return false;
      }
  
      public CarouselRun run() {
          if (!isRun) {
              isRun = true;
              return new CarouselRun();
          }
          return null;
      }
  }
  </code></pre>
  <p>Methods in CarouselRun:</p>
  <pre><code>public class CarouselRun {
      protected final int[] array = DecrementingCarousel.carousel.clone();
      protected int position = 0;
  
      public int next() {
          if (isFinished())
              return -1;
          else {
              while (array[position %= array.length] &lt;= 0) {
                  position++;
              }
          }
          return array[position++]--;
      }
  
      public boolean isFinished() {
          for (int el : array)
              if (el &gt; 0)
                  return false;
          return true;
      }
  }
  </code></pre>
  <p>So this subclass must decrement elements by gradually increasing decrement. When you need to decrement an element for the first time, decrease it by 1. Next time you need to decrement the same element, decrease it by 2. Next time decrease by 3, then by 4 and so on.</p>
  <pre><code>public class GraduallyDecreasingCarousel extends DecrementingCarousel{
      public GraduallyDecreasingCarousel(final int capacity) {
          super(capacity);
      }
  
      @Override
      public CarouselRun run() {
          if (!isRun) {
              isRun = true;
              return new GraduallyDecreasingCarouselRun();
          }
          return null;
      }
  }
  
  </code></pre>
  <pre><code>public class GraduallyDecreasingCarouselRun extends CarouselRun {
      int decrement = 1;
  
      @Override
      public int next() {
          int beforeDecreasing;
          if (isFinished())
              return -1;
          else {
              while (array[position %= array.length] &lt;= 0) {
                  position++;
                  if (position == array.length) {
                      decrement++;
                  }
              }
          }
          beforeDecreasing = array[position];
          array[position++] -= decrement;
          return beforeDecreasing;
      }
  </code></pre>
  <p>The main method:</p>
  <pre><code>public class Main {
      public static void main(String[] args) {
          DecrementingCarousel carousel = new GraduallyDecreasingCarousel(7);
  
          carousel.addElement(20);
          carousel.addElement(30);
          carousel.addElement(10);
  
          CarouselRun run = carousel.run();
  
          System.out.println(run.isFinished()); //false
  
          System.out.println(run.next()); //20
          System.out.println(run.next()); //30
          System.out.println(run.next()); //10
  
          System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);
  
          System.out.println(run.next()); //19
          System.out.println(run.next()); //29
          System.out.println(run.next()); //9
  
          System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);
  
          System.out.println(run.next()); //17
          System.out.println(run.next()); //27
          System.out.println(run.next()); //7
  
          System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);
  
          System.out.println(run.next()); //14
          System.out.println(run.next()); //24
          System.out.println(run.next()); //4
  
          System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);
  
          System.out.println(run.next()); //10
          System.out.println(run.next()); //20
  
          System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);
  
          System.out.println(run.next()); //5
          System.out.println(run.next()); //15
  
          System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);
  
          System.out.println(run.next()); //9
  
          System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);
  
          System.out.println(run.next()); //2
  
          System.out.println(run.isFinished()); //true
          System.out.println(run.next()); //-1
      }
  }
  </code></pre>
  <p>it works fine in main but wont pass the tests
  IDK how to change next method in GraduallyDecreasingCarouselRun</p>
  <p>tried counting next calls and reset the counter when its greater than amount of elements to decrease but when theres one element left its wasnt working correctly</p>