title: Below is the code for implementing a Queue using an array without using a counter for number of elements in it. Can someone please explain it to me?
body: <p>I've been trying to understand the code given below for a while. This is to implement Queue using array but without counting the elements inside the queue while inserting and deleting i.e. just by using rear and front indexes. Idea behind this was to increment the size of array by 1 so that the conditions for &quot;isFull()&quot; and &quot;isEmpty()&quot; are not the same.</p>
  <p>I want to know how adding 1 extra index is solving the problem of differentiating between full and empty. These methods don't make sense to me.</p>
  <pre><code>public class QueueWo {
  
      private int maxSize;
      private long[] arr;
      private int rear;
      private int front;
  
      public QueueWo(int s) {
          maxSize = s + 1;
          arr = new long[maxSize];
          rear = -1;
          front = 0;
      }
  
      public void insert(long j) {
          if(rear == maxSize - 1)
              rear = -1;
          arr[++rear] = j;
      }
  
      public long remove() {
          long temp = arr[front++];
          if(front == maxSize)
              front = 0;
          return temp;
      }
  
      public long peek() {
          return arr[front];
      }
  
      public boolean isEmpty() {
          return rear + 1 == front || front + maxSize - 1 == rear;
      }
  
      public boolean isFull() {
          return rear + 2 == front || front + maxSize - 2 == rear;
      }
  
      public int size() {
          if(rear &gt;= front)
              return rear - front + 1;
          else
              return (maxSize - front) + (rear + 1);
      }
  }
  </code></pre>