Skip to content

Latest commit

 

History

History
55 lines (36 loc) · 697 Bytes

File metadata and controls

55 lines (36 loc) · 697 Bytes

Queue using Array

Problem Link

Practice Problem


Pattern

  • Queue
  • Array

Approach

Use a circular array with front and rear pointers to support queue operations.


Time Complexity

O(1) per operation

Space Complexity

O(n)


Java Solution

class ArrayQueue {
    private int[] arr;
    private int front = 0, size = 0;

    public ArrayQueue(int capacity) {
        arr = new int[capacity];
    }

    public void enqueue(int val) {
        arr[(front + size) % arr.length] = val;
        size++;
    }

    public int dequeue() {
        int val = arr[front];
        front = (front + 1) % arr.length;
        size--;
        return val;
    }
}