class Queue { private int[] arr; // Array to store queue elements private int front; // Index of the front element private int rear; // Index of the last element private int size; // Current size of the queue private int capacity; // Maximum capacity of the queue // Constructor to initialize the queue public Queue(int capacity) { this.capacity = capacity; arr = new int[capacity]; front = -1; rear = -1; size = 0; } // Check if the queue is empty public boolean isEmpty() { return size == 0; } // Check if the queue is full public boolean isFull() { return size == capacity; } // Enqueue operation (add an element to the rear of the queue) public void enqueue(int value) { if (isFull()) { System.out.println("Queue is full!"); return; } // If the queue is empty, set front to 0 if (front == -1) { front = 0; } // Move rear to the next position (circular) rear = (rear + 1) % capacity; arr[rear] = value; // Add the new element to the rear size++; // Increment the size of the queue System.out.println("Enqueued: " + value); } //f=4 r=4 // 50 // Dequeue operation (remove an element from the front of the queue) public int dequeue() { if (isEmpty()) { System.out.println("Queue is empty!"); return -1; // Return a sentinel value for an empty queue } int dequeuedValue = arr[front]; // If there is only one element, reset the queue if (front == rear) { front = rear = -1; } else { // Move front to the next position (circular) front = (front + 1) % capacity; } size--; // Decrement the size of the queue return dequeuedValue; } // Display the elements of the queue public void displayQueue() { if (isEmpty()) { System.out.println("Queue is empty!"); return; } System.out.print("Queue: "); for (int i = 0; i < size; i++) { // Print elements in the queue from front to rear (circular) System.out.print(arr[(front + i) % capacity] + " "); } System.out.println(); } } public class QueueExample { public static void main(String[] args) { Queue queue = new Queue(5); // Create a queue with capacity 5 queue.enqueue(10); queue.enqueue(20); queue.enqueue(30); queue.displayQueue(); // Expected: 10 20 30 System.out.println("Dequeued: " + queue.dequeue()); // Expected: 10 queue.displayQueue(); // Expected: 20 30 queue.enqueue(40); queue.enqueue(50); queue.enqueue(60); // This will print "Queue is full!" queue.displayQueue(); // Expected: 20 30 40 50 } } ============================================================================================= )[]{} 2. if you encountorere closing brace when the stack is empty the return false 1. When ever you encounter an opening brace push on to the Stack 2. When ever you encounter a closing brace what is peak element popout that 3. After processing whole string if your Stack is empty then String is Balanced (]{} ============================================================================================= //{ Driver Code Starts //Initial Template for Java import java.io.*; import java.util.*; class GFG{ public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.nextLine()); while(t-- > 0){ String S = sc.nextLine().trim(); Solution ob = new Solution(); if(ob.valid(S)) System.out.println(1); else System.out.println(0); System.out.println("~"); } } } // } Driver Code Ends //User function Template for Java class Solution { boolean valid(String str) { // code here Stack s=new Stack<>(); for(int i=0;i