5 Jul 2019

Circular Queue

Problem Statement :
           Implement Circular Queue Using Arrays

Source Code Link
Solution:

Circular Queue is efficient version of Queue, in which elements are stored efficiently without wastage of space.
The concept that circular queue uses is incrementation of the index pointer, which is achieved by
index = (index +1 )%N , this formula brings the index to start position if it crosses the end of the array.

We will be using two pointers namely "rear" and "front".
rear pointer - tracks the latest item inserted.
front pointer - tracks the oldest item in the queue.

Below is the implementation of Circular Queue.


Now given below is implementation of all the operations present in the interface.

Any improvement or suggestions for the above code are most welcome. Source Code is hosted on Ideone , please follow the link given above.
Happy Coding!! :)

1 Jul 2019

Move all zeros to the end

Problem statement:
     Move all zeros towards the end in a given array.

Input : [0 1 2 0 3 0 0 4 0 0 0 5]

Soltion:

There are multiple approches to solve this problem, we will solve this problem using two pointers with
Space Complexity = O(1)
Time Complexity = O(n)

The idea here is to take two pointers one of which move at its speed and other one would count the number of non zero-elements.

Walk through the code for the above example.


Post your suggestions and comments.
Happy Coding !! :)