{"id":10030,"date":"2022-09-27T10:31:48","date_gmt":"2022-09-27T10:31:48","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=10030"},"modified":"2022-10-10T09:32:24","modified_gmt":"2022-10-10T09:32:24","slug":"tracking-the-current-maximum-element-in-the-stack","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/","title":{"rendered":"Tracking the Current Maximum Element in the Stack"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg\" alt=\"\" \/><\/p>\n<h3>Problem Statement:<\/h3>\n<p>You will be given a <a href=\"https:\/\/prepbytes.com\/blog\/category\/stacks\/\" title=\"stack\">stack<\/a> of integer values. You have to keep a track of the maximum value in the stack. For instance, the element that is currently at the top of the stack may be the maximum element in the stack. However, if that element is removed from the stack, the maximum value in the stack might now change. So, you always have to return the current maximum.<\/p>\n<h3>Example<\/h3>\n<p>Consider the stack given below. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184083773-1-01.png\" alt=\"\" \/><\/p>\n<p>In this stack, the maximum element is 50 which is also the top of the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184098280-1-02.png\" alt=\"\" \/><\/p>\n<p>Now, if we pop 50 from the top of the stack, the maximum element will now be 40. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184114929-1-03.png\" alt=\"\" \/><\/p>\n<p>If we pop an element from the top of the stack again, we still get 40 as the maximum value.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184125333-1-04.png\" alt=\"\" \/><\/p>\n<p>So, this is what you have to do. You just have to keep a track of the maximum element in the stack.<\/p>\n<h3>Brute Force Approach<\/h3>\n<p>Consider the stack shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184136030-1-05.png\" alt=\"\" \/><\/p>\n<p>In this track, if we have to return the maximum element, we will simply traverse the stack and find the maximum element of the stack.<\/p>\n<p>So, if we traverse this stack, we can say that 50 is the maximum element of the stack.<\/p>\n<p><strong>Note:<\/strong> In Java, we can traverse the stack using an iterator and the hasNext() method. In C++, we will have to create a copy of the original stack and pop all the elements from that copy stack to traverse it.<\/p>\n<p><strong>Time Complexity:<\/strong><\/p>\n<p>The time complexity for finding the maximum element one time is O(N) as we have to traverse the entire stack.<\/p>\n<p><strong>Space Complexity:<\/strong><\/p>\n<p>If we traverse the same stack using an iterator (in Java), the auxiliary space is O(1). In C++, we have to create a copy of the original stack and pop all the elements from it to traverse the stack. So, the space complexity, in that case, will be O(N).<\/p>\n<h3>Optimized Approach<\/h3>\n<p>In this approach, we will use an auxiliary stack that will maintain the maximum element at every stage. Consider that there are 2 stacks. We have the main stack and we have another stack called the max_stack. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184148089-1-06.png\" alt=\"\" \/><\/p>\n<p>So, when we insert an element into the main stack for the first time, we will also insert that element into the max_stack. So, if we insert 10, we insert 10 in both stacks.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184161690-1-07.png\" alt=\"\" \/><\/p>\n<p>Now, let us say that the next element is 20. So, we insert this element into the main stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184174436-1-08.png\" alt=\"\" \/><\/p>\n<p>Now, we will compare the current element 20 to the top of the max_stack and will store the maximum element on the top of the max_stack. So, 20 is greater than the current top of the max_stack i.e. 10. So, we will insert 20 in the max_stack also.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184187054-1-09.png\" alt=\"\" \/><\/p>\n<p>Let us say the next element is 5. So, we insert element 5 into the main stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184197820-1-10.png\" alt=\"\" \/><\/p>\n<p>Again, we compare the current element i.e. 5 to the top of the max stack. Since the top of the max_stack i.e. 20 is greater than the current element 5, we will insert 20 only into the max_stack.<\/p>\n<p>So, basically, the top of the max_stack shows the current maximum element of the stack. <\/p>\n<p>Now, let us insert 30 into the stack. So, you now understand that 30 will be inserted in the main stack and since it is also larger than the top of the max_stack, 30 will be inserted in the max_stack too.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184210771-1-11.png\" alt=\"\" \/><\/p>\n<p>Now, let us pop an element from the main stack.<\/p>\n<p>So, whenever you pop an element from the main stack, you have to pop an element from the max_stack too.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184222235-1-12.png\" alt=\"\" \/><\/p>\n<p>So, now after removing 30, the maximum value in the stack should be 20. If we see the top of the max_stack, it is also 20. So, the top of the max_stack always returns the maximum element in the main stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184234144-1-13.png\" alt=\"\" \/><\/p>\n<p>So, we hope that you have understood the procedure that we have to follow while pushing an element and popping an element from the stack.<\/p>\n<p>Now that we have understood the complete procedure, let us write the code for the same.<\/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_10031 {\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_10031 .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_10031 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_10031 .wpsm_nav-tabs > li.active > a, #tab_container_10031 .wpsm_nav-tabs > li.active > a:hover, #tab_container_10031 .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_10031 .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_10031 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_10031 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_10031 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_10031 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_10031 .wpsm_nav-tabs > li > a:hover , #tab_container_10031 .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_10031 .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_10031 .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_10031 .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_10031 .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_10031 .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_10031 .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_10031 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10031 .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_10031 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10031 .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_10031 .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_10031\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_10031\">\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_10031_1\" aria-controls=\"tabs_desc_10031_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_10031_2\" aria-controls=\"tabs_desc_10031_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>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_10031\">\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_10031_1\">\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 &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\nclass StackWithMax\r\n{\r\n\r\n\tstack&lt;int&gt; mainStack;\r\n\r\n\tstack&lt;int&gt; trackStack;\r\n\r\npublic:\r\n\tvoid push(int x)\r\n\t{\r\n\t\tmainStack.push(x);\r\n\t\tif (mainStack.size() == 1)\r\n\t\t{\r\n\t\t\ttrackStack.push(x);\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tif (x &gt; trackStack.top())\r\n\t\t\ttrackStack.push(x);\r\n\t\telse\r\n\t\t\ttrackStack.push(trackStack.top());\r\n\t}\r\n\r\n\tint getMax()\r\n\t{\r\n\t\treturn trackStack.top();\r\n\t}\r\n\r\n\tvoid pop()\r\n\t{\r\n\t\tmainStack.pop();\r\n\t\ttrackStack.pop();\r\n\t}\r\n};\r\n\r\nint main()\r\n{\r\n\tStackWithMax s;\r\n\ts.push(10);\r\n\tcout &lt;&lt; s.getMax() &lt;&lt; endl;\r\n\ts.push(20);\r\n\tcout &lt;&lt; s.getMax() &lt;&lt; endl;\r\n\ts.push(5);\r\n    cout &lt;&lt; s.getMax() &lt;&lt; endl;\r\n\ts.push(30);\r\n\tcout &lt;&lt; s.getMax() &lt;&lt; endl;\r\n\treturn 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_10031_2\">\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 Main {\r\n\r\nstatic class StackWithMax\r\n{\r\n\tstatic Stack&lt;Integer&gt; mainStack = new Stack&lt;Integer&gt; ();\r\n\r\n\tstatic Stack&lt;Integer&gt; trackStack = new Stack&lt;Integer&gt; ();\r\n\r\nstatic void push(int x)\r\n\t{\r\n\t\tmainStack.push(x);\r\n\t\tif (mainStack.size() == 1)\r\n\t\t{\r\n\t\t\ttrackStack.push(x);\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tif (x &gt; trackStack.peek())\r\n\t\t\ttrackStack.push(x);\r\n\t\telse\r\n\t\t\ttrackStack.push(trackStack.peek());\r\n\t}\r\n\r\n\tstatic int getMax()\r\n\t{\r\n\t\treturn trackStack.peek();\r\n\t}\r\n\r\n\tstatic void pop()\r\n\t{\r\n\t\tmainStack.pop();\r\n\t\ttrackStack.pop();\r\n\t}\r\n};\r\n\r\npublic static void main(String[] args)\r\n{\r\n\tStackWithMax s = new StackWithMax();\r\n\ts.push(10);\r\n\tSystem.out.println(s.getMax());\r\n\ts.push(20);\r\n\tSystem.out.println(s.getMax());\r\n\ts.push(5);\r\n    System.out.println(s.getMax());\r\n\ts.push(30);\r\n\tSystem.out.println(s.getMax());\r\n}\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_10031 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_10031 a\"),jQuery(\"#tab-content_10031\"));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>Time Complexity:<\/strong> The time complexity is O(1) as for the maximum element, we just have to peek at the top element of the max_stack.<\/p>\n<p><strong>Space Complexity (Auxiliary Space):<\/strong> The space complexity is O(N) as we have taken an auxiliary array to keep the track of the maximum element of the stack.<\/p>\n<p>We tried to discuss Tracking the Current Maximum Element in the Stack. We hope this article gives you a better understanding of Tracking the Current Maximum Element in the Stack, <a href=\"https:\/\/www.prepbytes.com\/\" title=\"PrepBytes\">PrepBytes<\/a> also provides a good collection of <a href=\"https:\/\/www.prepbytes.com\/prepbytes-courses\" title=\"Foundation Courses\">Foundation Courses<\/a> that can help you enhance your coding skills. Want to make sure you ace the interview in one go? Join our <a href=\"https:\/\/www.prepbytes.com\/placement-preparation-program\" title=\"Placement Program\">Placement Program<\/a> which will help you get prepared and land your dream job at MNCs. Mentors of Prepbytes are highly experienced and can provide you with basic, in-depth subject knowledge for better understanding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement: You will be given a stack of integer values. You have to keep a track of the maximum value in the stack. For instance, the element that is currently at the top of the stack may be the maximum element in the stack. However, if that element is removed from the stack, the [&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-10030","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>Tracking the Current Maximum Element in the Stack | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"We tried to discuss Tracking the Current Maximum Element in the Stack. We hope this article gives you a better understanding of Tracking the Current Maximum Element.\" \/>\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\/tracking-the-current-maximum-element-in-the-stack\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tracking the Current Maximum Element in the Stack | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"We tried to discuss Tracking the Current Maximum Element in the Stack. We hope this article gives you a better understanding of Tracking the Current Maximum Element.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-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=\"2022-09-27T10:31:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-10-10T09:32:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg\" \/>\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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Tracking the Current Maximum Element in the Stack\",\"datePublished\":\"2022-09-27T10:31:48+00:00\",\"dateModified\":\"2022-10-10T09:32:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/\"},\"wordCount\":834,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg\",\"articleSection\":[\"Stacks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/\",\"name\":\"Tracking the Current Maximum Element in the Stack | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg\",\"datePublished\":\"2022-09-27T10:31:48+00:00\",\"dateModified\":\"2022-10-10T09:32:24+00:00\",\"description\":\"We tried to discuss Tracking the Current Maximum Element in the Stack. We hope this article gives you a better understanding of Tracking the Current Maximum Element.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-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\":\"Tracking the Current Maximum Element in the 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":"Tracking the Current Maximum Element in the Stack | PrepBytes Blog","description":"We tried to discuss Tracking the Current Maximum Element in the Stack. We hope this article gives you a better understanding of Tracking the Current Maximum Element.","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\/tracking-the-current-maximum-element-in-the-stack\/","og_locale":"en_US","og_type":"article","og_title":"Tracking the Current Maximum Element in the Stack | PrepBytes Blog","og_description":"We tried to discuss Tracking the Current Maximum Element in the Stack. We hope this article gives you a better understanding of Tracking the Current Maximum Element.","og_url":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-09-27T10:31:48+00:00","article_modified_time":"2022-10-10T09:32:24+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Tracking the Current Maximum Element in the Stack","datePublished":"2022-09-27T10:31:48+00:00","dateModified":"2022-10-10T09:32:24+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/"},"wordCount":834,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg","articleSection":["Stacks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/","url":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/","name":"Tracking the Current Maximum Element in the Stack | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg","datePublished":"2022-09-27T10:31:48+00:00","dateModified":"2022-10-10T09:32:24+00:00","description":"We tried to discuss Tracking the Current Maximum Element in the Stack. We hope this article gives you a better understanding of Tracking the Current Maximum Element.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-stack\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664184247023-Topic.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/tracking-the-current-maximum-element-in-the-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":"Tracking the Current Maximum Element in the 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\/10030","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=10030"}],"version-history":[{"count":2,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/10030\/revisions"}],"predecessor-version":[{"id":10183,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/10030\/revisions\/10183"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=10030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=10030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=10030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}