{"id":1943,"date":"2020-07-01T09:45:53","date_gmt":"2020-07-01T09:45:53","guid":{"rendered":"https:\/\/blog.prepbytes.com\/?p=1943"},"modified":"2023-05-25T06:20:31","modified_gmt":"2023-05-25T06:20:31","slug":"get-minimum-element-from-stack","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/","title":{"rendered":"Get Minimum Element from Stack"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png\" alt=\"\" \/><\/p>\n<p>Get minimum element from stack is a common problem in computer science. A stack is a data structure that follows the Last-In-First-Out (LIFO) principle, where elements are added and removed from the top of the stack. While accessing and removing elements from the top of the stack is straightforward, finding the minimum element poses a challenge since the stack does not inherently provide direct access to it.<\/p>\n<h2>How to get Minimum Element from Stack<\/h2>\n<p>Design a Data Structure that performs the Stack operation like push(), pop() and one more operation getMin(), getMin() function should return the minimum element from the stack. The interviewer also told him that all these operations must be in O(1) time and use only stack data structure.<\/p>\n<ol>\n<li>push(x) &#8211; Push element x onto stack.<\/li>\n<li>pop() &#8211; Removes the element on top of the stack.<\/li>\n<li>top() &#8211; Get the top element.<\/li>\n<li>getMin() &#8211; Get the minimum element in the stack.<\/li>\n<\/ol>\n<p><a href=\"https:\/\/mycode.prepbytes.com\/problems\/stacks\/GETMINELEFST\" title=\"Go to mycode.prepbytes.com\" target=\"_blank\" rel=\"noopener noreferrer\"><u><strong><\/strong><\/u><\/a><\/p>\n<h3>STACK DATA STRUCTURE<\/h3>\n<p>An Abstract Data Type (ADT) that is often used in most programming languages is a stack. It is called a stack because it functions like a stack in the real world, such as a deck of cards, a pile of dishes, etc.<\/p>\n<p>A LIFO data structure is a stack. The acronym LIFO means last-in, first out. In this case, the piece that was added or inserted last is the one that gets accessible first. Insertion operations are referred to as PUSH operations and removal operations as POP operations in the context of stacks.<\/p>\n<p>The use of an array, structure, pointer, or linked list can be used to implement a stack. A stack may have a feeling of dynamic resizing or it may have a fixed size.<\/p>\n<p><strong>PUSH Operation:<\/strong><br \/>\nAdds an item in the stack. If the stack is full, then it is said to be an Overflow condition.<\/p>\n<p><strong>POP Operation:<\/strong><br \/>\nRemoves an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.<\/p>\n<p><strong>GET MINIMUM:<\/strong><br \/>\nRetrieve the minimum element in the stack.<\/p>\n<p><strong>TOP :<\/strong><br \/>\nGet the top element.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2020\/06\/getminelefst2.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_1946 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_1946 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_1946 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_1946 .wpsm_nav-tabs > li.active > a, #tab_container_1946 .wpsm_nav-tabs > li.active > a:hover, #tab_container_1946 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_1946 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_1946 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_1946 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_1946 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_1946 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_1946 .wpsm_nav-tabs > li > a:hover , #tab_container_1946 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_1946 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_1946 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_1946 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_1946 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_1946 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_1946 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_1946 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1946 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_1946 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1946 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_1946 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_1946\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_1946\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1946_1\" aria-controls=\"tabs_desc_1946_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1946_2\" aria-controls=\"tabs_desc_1946_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1946_3\" aria-controls=\"tabs_desc_1946_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_1946\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_1946_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include <stdio.h>\r\nint main()\r\n{\r\n    int q;\r\n    scanf(\"%d\",&q);\r\n    int stack[q],stackmin[q];\r\n    int top=-1,topmin=-1;\r\n    while(q--)\r\n    {\r\n        int x;scanf(\"%d\",&x);\r\n        if(x==1)\r\n        {\r\n            int y;scanf(\"%d\",&y);\r\n            stack[++top]=y;\r\n            if(topmin==-1)\r\n              stackmin[++topmin]=y;\r\n            else if(y<=stackmin[topmin])\r\n                stackmin[++topmin]=y;\r\n        }\r\n        else if(x==2)\r\n        {\r\n            if(top==-1)\r\n                printf(\"-1&#92;n\");\r\n            else\r\n            {\r\n                if(stack[top]==stackmin[topmin])\r\n                    topmin--;\r\n                \/\/printf(\"%d&#92;n\",stack[top]);\r\n                top--;}\r\n        }\r\n        else if(x==3)\r\n        {\r\n            if(top==-1)\r\n                printf(\"-1&#92;n\");\r\n            else\r\n                printf(\"%d&#92;n\",stack[top]);}\r\n            else\r\n            {\r\n                if(top==-1)\r\n                    printf(\"-1&#92;n\");\r\n                else\r\n                    printf(\"%d&#92;n\",stackmin[topmin]);}\r\n    }\r\n    return 0;\r\n }\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_1946_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include <bits\/stdc++.h>\r\nusing namespace std;\r\nstack<int> s;\r\nint minEle;\r\nvoid Push(int x)\r\n{\r\n    if (s.empty()) {\r\n        s.push(x);\r\n        minEle = x;\r\n    }\r\n    else if (x > minEle) {\r\n    s.push(x);\r\n    }\r\n    else {\r\n        s.push(2 * x - minEle);\r\n        minEle = x;\r\n    }\r\n}\r\nvoid Pop()\r\n{\r\n    if (s.empty()) {\r\n        cout << -1 << '&#92;n';\r\n    }\r\n    else{\r\n        int top = s.top();\r\n        if (top < minEle)\r\n            minEle = 2 * minEle - top;\r\n        s.pop();\r\n     }\r\n}\r\nint minimum()\r\n{\r\n    if(!s.empty())\r\n        return minEle;\r\n    else\r\n        return -1;\r\n}\r\nint Top()\r\n{\r\n    if(s.empty())\r\n        return -1;\r\n    else{\r\n    int t = s.top(); \/\/ Top element.\r\n    \/\/ If t < minEle means minEle stores\r\n    \/\/ value of t.\r\n    return (t < minEle)? minEle:t;\r\n    }\r\n}   \r\nint main()\r\n{\r\n    int q;\r\n    cin>>q;\r\n    while(q--){\r\n\r\n    int k;\r\n    cin>>k;\r\n    if(k==1){\r\n        int x;\r\n        cin>>x;\r\n        Push(x);\r\n    }\r\n\r\n    else if(k==2)\r\n        Pop();\r\n    else if(k==3)\r\n        cout<<Top()<<endl;\r\n    else if(k==4)\r\n        cout<<minimum()<<endl;       \r\n    }\r\nreturn 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_1946_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*;\r\nclass solution{\r\n   static int minEle;\r\n   static Stack <Integer> s=new Stack<>();\r\n   static void Push(int x)\r\n   {\r\n       if (s.isEmpty()==true) {\r\n           s.push(x);\r\n           minEle = x;\r\n       }\r\n       else if (x > minEle) {\r\n           s.push(x);\r\n       }\r\n       else {\r\n           s.push(2 * x - minEle);\r\n           minEle = x;\r\n       }\r\n   }\r\n   static void Pop()\r\n   {\r\n       if (s.isEmpty()==true) {\r\n           System.out.println(\"-1\");\r\n       }\r\n       else{\r\n           int top = s.peek();\r\n           if (top < minEle)\r\n               minEle = 2 * minEle - top;\r\n           s.pop();\r\n       }\r\n   }\r\n   static int minimum()\r\n   {\r\n       if(!s.isEmpty()==true)\r\n           return minEle;\r\n       else\r\n           return -1;\r\n   }\r\n   static int Top()\r\n   {\r\n       if(s.isEmpty()==true)\r\n           return -1;\r\n       else{\r\n       int t = s.peek(); \/\/ Top element.\r\n       \/\/ If t < minEle means minEle stores\r\n       \/\/ value of t.\r\n       return (t < minEle)? minEle:t;\r\n       }\r\n   }\r\n   public static void main (String[] args) {\r\n       Scanner sc=new Scanner(System.in);\r\n       int q=sc.nextInt();\r\n       while(q-->0){\r\n           int k=sc.nextInt();\r\n           if(k==1){\r\n               int x=sc.nextInt();\r\n               Push(x);\r\n           }\r\n\r\n           else if(k==2)\r\n               Pop();\r\n           else if(k==3)\r\n               System.out.println(Top());\r\n           else if(k==4)\r\n            System.out.println(minimum());     \r\n       }\r\n       }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_1946 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_1946 a\"),jQuery(\"#tab-content_1946\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Conclusion<\/strong><br \/>\nThe &quot;Minimum Element Stack&quot; or &quot;Min Stack&quot; approach provides an efficient solution to retrieve the minimum element from a stack. By maintaining an additional stack, the min stack, alongside the main stack, we can keep track of the minimum element at each point.<\/p>\n<p>It&#8217;s worth noting that the Min Stack approach introduces additional space complexity due to the requirement of an extra stack. However, the trade-off is acceptable considering the constant-time retrieval of the minimum element.<\/p>\n<p>The Min Stack approach provides an effective solution for obtaining the minimum element from a stack. By maintaining a separate stack to track the minimums, we can efficiently retrieve the minimum element without compromising the fundamental properties of a stack data structure.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<p><strong>Q1. Why is it necessary to use an additional stack for retrieving the minimum element?<\/strong><br \/>\n<strong>Ans.<\/strong> The additional stack, known as the min stack, is used to keep track of the minimum element at each point in the main stack. It ensures constant-time retrieval of the minimum element without having to traverse the entire stack every time. This approach improves efficiency and maintains the LIFO property of the stack.<\/p>\n<p><strong>Q2. What is the time complexity of retrieving the minimum element from the stack using the Min Stack approach?<\/strong><br \/>\n<strong>Ans.<\/strong> The time complexity to retrieve the minimum element using the Min Stack approach is constant, O(1). Since the minimum element is always stored at the top of the min stack, accessing it does not depend on the size of the main stack.<\/p>\n<p><strong>Q3. Does using the Min Stack approach increase the space complexity?<\/strong><br \/>\n<strong>Ans.<\/strong> Yes, using the Min Stack approach increases the space complexity. In addition to the main stack, an extra stack (the min stack) is required to store the minimum elements. However, the space complexity remains proportional to the number of elements in the stack.<\/p>\n<p><strong>Q4. How does the Min Stack approach handle scenarios when the minimum element is popped from the main stack?<\/strong><br \/>\n<strong>Ans.<\/strong> When the minimum element is popped from the main stack, the Min Stack approach ensures consistency by also popping the corresponding minimum element from the min stack. This guarantees that the top of the min stack always represents the minimum element present in the main stack at any given time.<\/p>\n<p><strong>Q5. Can the Min Stack approach be used with any type of stack implementation?<\/strong><br \/>\n<strong>Ans.<\/strong> Yes, the Min Stack approach can be used with any type of stack implementation, whether it&#8217;s implemented using an array or a linked list. The key idea is to maintain a separate stack for tracking the minimum elements alongside the main stack.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get minimum element from stack is a common problem in computer science. A stack is a data structure that follows the Last-In-First-Out (LIFO) principle, where elements are added and removed from the top of the stack. While accessing and removing elements from the top of the stack is straightforward, finding the minimum element poses a [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[127],"tags":[],"class_list":["post-1943","post","type-post","status-publish","format-standard","hentry","category-stacks"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Stacks | Get Minimum Element from Stack | Prepbytes<\/title>\n<meta name=\"description\" content=\"A Stack - Abstract Data Type (adt), Commonly Used in Most Programming Languages. Stack as it Behaves Like a Real-world Stack,example \u2013 a Deck of Cards or a Pile of Plates.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Stacks | Get Minimum Element from Stack | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"A Stack - Abstract Data Type (adt), Commonly Used in Most Programming Languages. Stack as it Behaves Like a Real-world Stack,example \u2013 a Deck of Cards or a Pile of Plates.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-01T09:45:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-25T06:20:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Get Minimum Element from Stack\",\"datePublished\":\"2020-07-01T09:45:53+00:00\",\"dateModified\":\"2023-05-25T06:20:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/\"},\"wordCount\":805,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png\",\"articleSection\":[\"Stacks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/\",\"name\":\"Stacks | Get Minimum Element from Stack | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png\",\"datePublished\":\"2020-07-01T09:45:53+00:00\",\"dateModified\":\"2023-05-25T06:20:31+00:00\",\"description\":\"A Stack - Abstract Data Type (adt), Commonly Used in Most Programming Languages. Stack as it Behaves Like a Real-world Stack,example \u2013 a Deck of Cards or a Pile of Plates.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Stacks\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/stacks\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Get Minimum Element from Stack\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Stacks | Get Minimum Element from Stack | Prepbytes","description":"A Stack - Abstract Data Type (adt), Commonly Used in Most Programming Languages. Stack as it Behaves Like a Real-world Stack,example \u2013 a Deck of Cards or a Pile of Plates.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/","og_locale":"en_US","og_type":"article","og_title":"Stacks | Get Minimum Element from Stack | Prepbytes","og_description":"A Stack - Abstract Data Type (adt), Commonly Used in Most Programming Languages. Stack as it Behaves Like a Real-world Stack,example \u2013 a Deck of Cards or a Pile of Plates.","og_url":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2020-07-01T09:45:53+00:00","article_modified_time":"2023-05-25T06:20:31+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Get Minimum Element from Stack","datePublished":"2020-07-01T09:45:53+00:00","dateModified":"2023-05-25T06:20:31+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/"},"wordCount":805,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png","articleSection":["Stacks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/","url":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/","name":"Stacks | Get Minimum Element from Stack | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png","datePublished":"2020-07-01T09:45:53+00:00","dateModified":"2023-05-25T06:20:31+00:00","description":"A Stack - Abstract Data Type (adt), Commonly Used in Most Programming Languages. Stack as it Behaves Like a Real-world Stack,example \u2013 a Deck of Cards or a Pile of Plates.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100951334-Article_344.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/get-minimum-element-from-stack\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Stacks","item":"https:\/\/prepbytes.com\/blog\/category\/stacks\/"},{"@type":"ListItem","position":3,"name":"Get Minimum Element from Stack"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=1943"}],"version-history":[{"count":8,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1943\/revisions"}],"predecessor-version":[{"id":16535,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1943\/revisions\/16535"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=1943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=1943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=1943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}