In C++ deque class is also called a double-ended queue class. It provides many functions to manage elements from both ends.
Below are some of the frequently used functions:
- empty(): to check if the deque instance is empty or not
- front(): returns the first element.
- back(): returns the last element.
- push_back(): adds the element to the end of the list.
- push_front(): adds the element to the beginning of the list.
- size() : returns the number of elements.
- clear(): removes all the elements.
- pop_back(): removes the last element
- pop_front(): removes the first element
Using deque, stack data structure can be implemented. eg. by using push_back() and pop_back() methods.
Using deque, queue data structure also can be implemented. eg. by using push_back() and pop_front() methods.
Below is one example problem solved using deque class:
This problem can be solved using the sliding window approach and Queue data structure:
- While iterating over the elements, push its index to the Queue if there is a negative element.
- Window size is k elements. Remove any element in the queue that is not part of the window.
- After iterating over first k-1 elements, check for any element in the queue, add it to the result list, or add ‘0’ to the result list.
Here is the solution:
Introducing Jyo Services — Your Go-To Partner for Tech Solutions
Are you looking to boost your career or bring your tech ideas to life? Jyo Services offers a range of specialized solutions tailored just for you:
- One-on-One Interview Preparation: Tailored coaching for freshers and professionals with up to 7 years of software development experience.
- Custom Software Development: Expertise in embedded technologies, AI/ML, Gen AI, Cyber Security, Linux-based applications, and network protocol developments.
- Patent Assistance: Comprehensive support for drafting, filing and maintaining patents in India.
- College Projects: Professional guidance for Engineering students on academic projects.
Unlock your potential with Jyo Services! Contact us today to get started.
DM me at: www.linkedin.com/in/jyothivs or jyos.v.s@gmail.com