Posts

Implementing Circular Queue in java

               A queue which has connected front end and rear end to form a circle. Let us implement this in java. Front end is the one where we remove the element. Rear end is the one where the data is inserted. It has 5 operations list below. 1.        Initialize 2.        Enqueue 3.        Dequeue 4.        isEmpty() 5.        isFull() 1.initialize: The data structure ‘CircularQueue’ size is assigned with value ‘k’. 2.Enqueue: It inserts the elements to the rear end. If the queue is full,it gives you an exception. 3.Dequeue: It deletes the element in the front end. It gives you an exception,when your queue is empty. 4.isEmpty(): it checks for queue has no elements. 5.isFull(): It checks the queue is full or not. Program: public class CircularQueueEg {   ...

Java Program to find the K largest elements in the given array

  Problem : To find the K largest elements in the given array. Implementation: ·        Let us create a min heap as priority queue with a size of K. ·        Process the elements in heap. If the heap size exceeds the value of ‘K’,then the smallest element. ·        Convert the heap into array and display the two largest values. Program: import java.util.PriorityQueue; public class KLargestCode {     public static int[] identifyKLargest(int[] nos, int k) {         if (nos == null || nos.length == 0 || k <= 0) {             return new int[0];         }         PriorityQueue<Integer> minHeap1 = new PriorityQueue<>(k);       //Let us process the elements in he...

PriorityQueue in Java

 A queue data structure that retrieves the data element based on priority. The priority can be ahighest or lowest. It can implemented as “queue” interface in java. How it orders the elements? It follows natural for ordering. For numbers, it follows ascending order. For strings, it follows lexicographic order. Program: Built in packages: java.uti.PriorityQueue, java.util.Comparator; Steps to follow: ·        Create an object for PriorityQueue. ·        Insert the elements to the priority queue and display it. ·        Delete the head of the queue and displayed it. ·        The head of the queue is displayed, it is retrieved but not deleted. ·        Display the PriorityQueue after polling. ·        Let us develop a PriorityQueue with comparator in the descending order and display it. ...

Design a data structure to find minimum and maximum value in java

Problem : Design a data structure to find minimum and maximum value in the structure in an efficient way. How to implement it?   Create a deque with a stack. Program: ·        First, include the built in packages java.util.Deque, java.util.LinkedList; ·        Write a class “stackDeque”. Develop three deque objects. One is for stack; remaining twos are minimum and maximum stack. ·        Write the functions for Push and pop. ·        Minimum stack and maximum stack implementation are written separately. ·        Finally, call the functions to display the minimum and maximum values in the data structure. Java Program: import java.util.Deque; import java.util.LinkedList; public class stackDeque {     private Deque<Integer> stack1;     private Deque<Integer> minStackDe...

java implementation of Deque

 Deque(double ended Queue) is a type of queue. The elements can be inserted and deleted in both the ends. Deque is implemented in java by the concept of ‘ArrayDeque’. What is ArrayDeque?               It is a class available in java. ArrayDeque -Array Double Ended Queue which implement deque in java. It creates a resizable array for storing the queue elements. Features of ArrayDeque: ·        It holds light weight memory. ·        Fast access to elements. ·        Time performance is constant. ·        Resizable. Implementation of deque in java: It uses built in packages java.util.ArrayDeque, java.util.deque. Steps to follow: Creation: An object is created for ‘ArrayDeque’. Insertion: To insert the data, ‘addFirst’,’addLast’,’offerFirst’ ,’offerLast’ functions are used. Removal:...

Generate numbers (Permutation) in java

               Numbers are magical elements in the field of mathematics. Generating all possible number combinations from a set of numbers is permutation. Let us generate numbers based on this. How to generate numbers like permutation in java? Let us create this program using an integer array. Steps: à Main method: Include the built in packages java.util.ArrayList and java.util.List. First, Write the code for class with main() function. Next, Declare an integer array with 3 integers as input. Now, call a function generateNos() with input array as arguments. Finally, print the number received from the function. à generateNos(): It create a list(output) for display. It has array as Boolean which stores the   ip_used. Call the backtrack_it function.It generates the number combination (Permutation). à backtrack_it(): It checks the current number combination is same as the ip_digits, it is added to output. Each numbe...

Reverse K elements problem solution in java

Problem :  You have a ‘n’ number of elements. You want to reverse a set of elements. Let it be ‘K’ number of elements. How will you solve using a queue and stack. Implement it in java. Solution in java: ·        Create a public class with main() function. ·        A member function with the input parameters of queue and the ‘k’ value. It checks whether the queue is null or queue’s size is less than k or k is less than 0. ·        If any one of the above is true, it returns the queue. ·        Create an integer stack. Push ‘n’ number of elements into the stack. ·        Delete(enque) the ‘K’ number of elements and store it into queue. ·        Move the remaining elements from the front end to the rear end to preserve the order. Program: import java.util.LinkedList; import java.util.Queu...