{"id":2893,"date":"2021-10-25T12:28:59","date_gmt":"2021-10-25T06:58:59","guid":{"rendered":"https:\/\/www.interviewbit.com\/blog\/?p=2893"},"modified":"2021-10-25T12:29:03","modified_gmt":"2021-10-25T06:59:03","slug":"next-greater-element","status":"publish","type":"post","link":"https:\/\/www.interviewbit.com\/blog\/next-greater-element\/","title":{"rendered":"Next Greater Element"},"content":{"rendered":"\n<div class=\"gutentoc tocactive nostyle\" style=\"max-width:400px\"><div class=\"gutentoc-toc-wrap\"><div class=\"gutentoc-toc-title-wrap\"><div class=\"gutentoc-toc-title\">Table Of Contents<\/div><div id=\"open\" class=\"toggletwo\">show<\/div><\/div><div id=\"toclist\"><div class=\"gutentoc-toc__list-wrap\"><ul class=\"gutentoc-toc__list\"><li><a href=\"#problem-statement\">Problem Statement<\/a><\/li><li><a href=\"#brute-force-approach\">Brute Force Approach<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#c-code\">C++ Code<\/a><\/li><li><a href=\"#java-code\">Java Code<\/a><\/li><li><a href=\"#python-code\">Python Code<\/a><\/li><\/ul><li><a href=\"#efficient-approach-using-stack\">Efficient Approach: Using Stack<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#c-code-for-stack-method\">C++ Code For Stack Method<\/a><\/li><li><a href=\"#java-code-for-stack-method\">Java Code For Stack Method<\/a><\/li><li><a href=\"#python-code-for-stack-method\">Python Code For Stack Method<\/a><\/li><\/ul><li><a href=\"#practice-question\">Practice Question<\/a><\/li><li><a href=\"#faqs\">FAQs<\/a><\/li><\/ul><\/div><\/div><\/div><\/div>\n\n\n\n<h2 id=\"problem-statement\">Problem Statement<\/h2>\n\n\n\n<p>Given an array <strong>A[]<\/strong>, find the <strong>next greater element<\/strong><strong>G[i]<\/strong> for every element <strong>A[i]<\/strong> in the array.<br>The Next greater Element for an element A[i] is the first greater element on the right side of A[i] in the array. Elements for which no greater element exists, consider the next greater element as <strong>-1<\/strong>.<\/p>\n\n\n\n<p>More formally,<\/p>\n\n\n\n<p><strong>G[i]<\/strong> for an element <strong>A[i]<\/strong> = an element <strong>A[j]<\/strong> such that <strong>j<\/strong> is minimum<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; possible AND <strong>j &gt; i<\/strong> AND <strong>A[j] &gt; A[i]<\/strong><\/p>\n\n\n\n<p><strong>Examples:<\/strong><\/p>\n\n\n\n<p><strong>Input: <\/strong>A[] = [8, 5, 2, 3, 9]<br><strong>Output: <\/strong>[9, 9, 3, 9, -1]<br><strong>Explanation:<\/strong><\/p>\n\n\n\n<ol><li>The least greater element of 8 on its right is 9<\/li><li>The least greater element of 5 on its right is 9.<\/li><li>The least greater element of 2 on its right is 3.<\/li><li>The least greater element of 3 on its right is 9.<\/li><li>No element is greater than 9 on its right, hence -1.<\/li><\/ol>\n\n\n\n<p><strong>Input: <\/strong>&nbsp;A[] = [18, 25, 2, 13, 29]<br><strong>Output:<\/strong> [25, 29, 13, 29, -1]\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-black-background-color has-black-color is-style-wide\"\/>\n\n\n\n<h2 id=\"brute-force-approach\">Brute Force Approach<\/h2>\n\n\n\n<p>A simple approach to solving the problem is to run two nested loops and for each element <strong>A[i]<\/strong> find the first element to its right strictly greater than it.<\/p>\n\n\n\n<p><strong>Algorithm<\/strong><\/p>\n\n\n\n<ul><li>Iterate loop <strong>i<\/strong> from <strong>1<\/strong> till <strong>N<\/strong>.<\/li><li>Initialise a variable <strong>next_greater = -1<\/strong>.<\/li><li>Iterate a loop <strong>j<\/strong> from <strong>i + 1<\/strong> till <strong>N<\/strong> and perform the following:<ul><li>If <strong>A[j] &gt; A[i]:<\/strong><ul><li><strong>next_greater = A[j]<\/strong> and break.<\/li><\/ul><\/li><li>Else, continue<\/li><\/ul><\/li><li>Print the value <strong>next_greater<\/strong> obtained.<\/li><\/ul>\n\n\n\n<h3 id=\"c-code\">C++ Code<\/h3>\n\n\n\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=\"\">void printNGE(int arr[], int n) {\n  int next, i, j;\n  for (i = 0; i &lt; n; i++) {\n    next = -1;\n    for (j = i + 1; j &lt; n; j++) {\n      if (arr[i] &lt; arr[j]) {\n        next = arr[j];\n        break;\n      }\n    }\n    cout &lt;&lt; arr[i] &lt;&lt; \" -- \" &lt;&lt;\n      next &lt;&lt; endl;\n  }\n}<\/pre>\n\n\n\n<h3 id=\"java-code\">Java Code<\/h3>\n\n\n\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=\"\"> static void printNGE(int arr[], int n) {\n  int next, i, j;\n  for (i = 0; i &lt; n; i++) {\n    next = -1;\n    for (j = i + 1; j &lt; n; j++) {\n      if (arr[i] &lt; arr[j]) {\n        next = arr[j];\n        break;\n      }\n    }\n    System.out.println(arr[i] + \" -- \" + next);\n  }\n}<\/pre>\n\n\n\n<h3 id=\"python-code\">Python Code<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def printNGE(arr):\n \n    for i in range(0, len(arr), 1):\n \n        next = -1\n        for j in range(i + 1, len(arr), 1):\n            if arr[i] &lt; arr[j]:\n                next = arr[j]\n                break\n \n        print(str(arr[i]) + \" -- \" + str(next))<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-black-background-color has-black-color is-style-wide\"\/>\n\n\n\n<h2 id=\"efficient-approach-using-stack\">Efficient Approach: Using Stack<\/h2>\n\n\n\n<p>The idea is to use a stack to solve this problem. Push the first element of the array into the stack. Now iterate through all the elements from <strong>1<\/strong> to <strong>N &#8211; 1<\/strong> and if the value of the current index is smaller than the value at the top of the stack, push it into the stack. Similarly, if the value of the current index is greater than the value at the top of the stack, pop all remaining elements, and the <strong>next greater element <\/strong>is the popped elements.&nbsp;<\/p>\n\n\n\n<p><strong>Dry Run of the above approach<\/strong><\/p>\n\n\n\n<ul><li>Let the initial array be <strong>Arr[] = {11, 13, 21, 3, 4, 2}<\/strong><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"initial array\"  class=\" pk-lazyload\"  data-pk-sizes=\"auto\"  data-pk-src=\"https:\/\/lh3.googleusercontent.com\/jedz2ssGl9MXf3yy9fs07b887JpcuKnggIjVcuz3eUTGLss2nEdfUzz6R9UQ7jotreadUY5NVdDAKmrgwRn26To_Nnq19nCNoSIGqQv4BsZ8xDZbqDwvHlolcDc3hNgM1hSOk0x3=s1600\" ><\/figure>\n\n\n\n<ul><li>Push the first element into the stack.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Push the first element into the stack\"  class=\" pk-lazyload\"  data-pk-sizes=\"auto\"  data-pk-src=\"https:\/\/lh5.googleusercontent.com\/mPkwam-YDiRGP7nhoDgKynie6rDw6pSDkrzvwGPXZzipqR3QqQ87Jl2xTkpEitpm93O3trFTJIZYSOoYSPo_6Zt6VyvKY_6bhuv5UMSsAuwX8pew_oCpeQHlOFYYYafu1gR20mRN=s1600\" ><\/figure>\n\n\n\n<ul><li>Traverse the array from <strong>i = 1<\/strong> till <strong>i = N &#8211; 1. <\/strong>Since, <strong>13 &gt; 11<\/strong>, pop out 11 from the stack and set 13 as the <strong>next greater element of 11.<\/strong><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Traverse the array \"  class=\" pk-lazyload\"  data-pk-sizes=\"auto\"  data-pk-src=\"https:\/\/lh3.googleusercontent.com\/vAVt6ImWAUgtzTT3JoZIhmkwcAnlhAv8fw0GZBmfxEwKjai77ACw3DqLWyt6lx8wvdF0Mk21YvIrTnumM9dT5VCoSJQHQIxBS3WGu2Fu5Ts9EX8O9ySDCPwXsLBn9DgBfYof2DDu=s1600\" ><\/figure>\n\n\n\n<ul><li>Similarly, repeat the above step again. Continue loop from <strong>2<\/strong> to <strong>N &#8211; 1. <\/strong>Since, <strong>21 &gt; 13<\/strong>, pop out 13 from the stack and set 21 as the <strong>next greater element of 13.<\/strong><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Continue loop from 2 to N - 1\"  class=\" pk-lazyload\"  data-pk-sizes=\"auto\"  data-pk-src=\"https:\/\/lh3.googleusercontent.com\/Wrraw6dc1IfHheQ_LqPPgonb9b1DnYUZ7qVWWt2GzhnNTknGFOlTPQZsc09fBhKOQ7Tr8T8tKPYVclR0339Wq3IGf9qeOwlHHu2LCcD_yUenUqd0T0i66H-4Wc8ejFGQhuWgcdUt=s1600\" ><\/figure>\n\n\n\n<ul><li>Continuing the loop from <strong>i = 3<\/strong>. Since, the element 3 &lt; 21, push it into the stack.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Continuing the loop from i = 3\"  class=\" pk-lazyload\"  data-pk-sizes=\"auto\"  data-pk-src=\"https:\/\/lh4.googleusercontent.com\/KVMBFlnxTnxlrZ3AeNRYr5IPw4bV27026YevVpket1sn8_7QM2ZqI2GK30QncTZ08fVZaWuh_zTwRPD9NS8Jhf3XDYPCC9ov89P992LA3PHRc5qzLdYY6fU2Y5tWtPZojy6F0xUB=s1600\" ><\/figure>\n\n\n\n<ul><li>Continuing the loop from <strong>i = 4<\/strong>. Since, the 4 &gt; 3, pop <strong>3<\/strong> and set <strong>next greater element <\/strong>of 3 as <strong>4<\/strong>.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Continuing the loop from i = 4\"  class=\" pk-lazyload\"  data-pk-sizes=\"auto\"  data-pk-src=\"https:\/\/lh6.googleusercontent.com\/x6TfCUMQuKk9gCu5MxjTl2P6rFzBp3fntx07qvclDz3Ktni-zVuNiSwjTCoKvhtKF4vnGSXjKH3IFUEf8ce7_RcX30o5MT5DfM3TMCR9JqokN2ddrw-KUFh-tqmEKP_kq3Ekn6U_=s1600\" ><\/figure>\n\n\n\n<ul><li>Continuing the loop from <strong>i = 5<\/strong>. Since, the element 2 &lt; 4, push it into the stack.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Continuing the loop from i = 5\"  class=\" pk-lazyload\"  data-pk-sizes=\"auto\"  data-pk-src=\"https:\/\/lh6.googleusercontent.com\/6fX5pTYQmnH4sqHZJMj-TD5w83QjQA0RCe14xMu7Xi-YKPC1-fDcX825zt8JXmy2IaCJys8VuzBZ_jyDZhqsdx3F28nFkg_9gvLYY-dx2hVl6uOPbPECvjS5kS6GgDZ008NukJQG=s1600\" ><\/figure>\n\n\n\n<ul><li>Now, since the whole array has been traversed, still we are left with elements in the stack. This means, for all these elements, there doesn\u2019t exist any <strong>greater element <\/strong>to its right. Hence, we mark them as <strong>-1<\/strong>.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"whole array has been traversed\"  class=\" pk-lazyload\"  data-pk-sizes=\"auto\"  data-pk-src=\"https:\/\/lh6.googleusercontent.com\/fY-m0r4z0nXSI7_dhPt1qjqQ6T9DjYC9vSyZDpG4EvpWh45Uof-FvRZoZr4I4mU3SDzv3Ccj2bOJknkVySdrPoqWTsgTZ1WaAU0unXIEY82vaYtrPGQCGzkanfUbaEHpY18qw_yw=s1600\" ><\/figure>\n\n\n\n<p><strong>Algorithm<\/strong><\/p>\n\n\n\n<ul><li>Initialise a stack <strong>st<\/strong>.<\/li><li>Push the first element of the array into the stack.<\/li><li>Iterate from <strong>i = 1<\/strong> till <strong>i = N &#8211; 1<\/strong> and for each <strong>i<\/strong>, check whether the current element:<ul><li><strong>A[i] &gt; st.top()<\/strong>, keep popping elements from the stack, until an element greater than <strong>A[i]<\/strong> appears in the stack. The current element <strong>A[i]<\/strong> is the <strong>next greater element <\/strong>for each of the popped elements.<\/li><li><strong>A[i] &lt; st.top()<\/strong>, push it into the stack.<\/li><\/ul><\/li><li>Continue traversing till the end of the array.<\/li><li>At last, pop out the remaining elements of the stack and print <strong>-1<\/strong>, since no<strong> next greater element <\/strong>exists for them.<\/li><\/ul>\n\n\n\n<h3 id=\"c-code-for-stack-method\">C++ Code For Stack Method<\/h3>\n\n\n\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=\"\">void printNGE(int arr[], int n) {\n  stack &lt; int > s;\n  s.push(arr[0]);\n \n  for (int i = 1; i &lt; n; i++) {\n \n    if (s.empty()) {\n      s.push(arr[i]);\n      continue;\n    }\n    while (s.empty() == false &amp;&amp;\n      s.top() &lt; arr[i]) {\n      cout &lt;&lt; s.top() &lt;&lt;\n        \" --> \" &lt;&lt; arr[i] &lt;&lt; endl;\n      s.pop();\n    }\n    s.push(arr[i]);\n  }\n  while (s.empty() == false) {\n    cout &lt;&lt; s.top() &lt;&lt; \" --> \" &lt;&lt; -1 &lt;&lt; endl;\n    s.pop();\n  }\n}<\/pre>\n\n\n\n<h3 id=\"java-code-for-stack-method\">Java Code For Stack Method<\/h3>\n\n\n\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=\"\">static class stack {\n  int top;\n  int items[] = new int[100];\n  void push(int x) {\n    if (top == 99) {\n      System.out.println(\"Stack full\");\n    } else {\n      items[++top] = x;\n    }\n  }\n \n  int pop() {\n    if (top == -1) {\n      System.out.println(\"Underflow error\");\n      return -1;\n    } else {\n      int element = items[top];\n      top--;\n      return element;\n    }\n  }\n \n  boolean isEmpty() {\n    return (top == -1) ? true : false;\n  }\n}\nstatic void printNGE(int arr[], int n) {\n  int i = 0;\n  stack s = new stack();\n  s.top = -1;\n  int element, next;\n  s.push(arr[0]);\n \n  for (i = 1; i &lt; n; i++) {\n    next = arr[i];\n \n    if (s.isEmpty() == false) {\n      element = s.pop();\n \n      while (element &lt; next) {\n        System.out.println(element + \" --> \" +\n          next);\n        if (s.isEmpty() == true)\n          break;\n        element = s.pop();\n      }\n \n      if (element > next)\n        s.push(element);\n    }\n \n    s.push(next);\n  }\n  while (s.isEmpty() == false) {\n    element = s.pop();\n    next = -1;\n    System.out.println(element + \" -- \" + next);\n  }\n}<\/pre>\n\n\n\n<h3 id=\"python-code-for-stack-method\">Python Code For Stack Method<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\n&lt;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=\"\">static class stack {\n  int top;\n  int items[] = new int[100];\n  void push(int x) {\n    if (top == 99) {\n      System.out.println(\"Stack full\");\n    } else {\n      items[++top] = x;\n    }\n  }\n \n  int pop() {\n    if (top == -1) {\n      System.out.println(\"Underflow error\");\n      return -1;\n    } else {\n      int element = items[top];\n      top--;\n      return element;\n    }\n  }\n \n  boolean isEmpty() {\n    return (top == -1) ? true : false;\n  }\n}\nstatic void printNGE(int arr[], int n) {\n  int i = 0;\n  stack s = new stack();\n  s.top = -1;\n  int element, next;\n  s.push(arr[0]);\n \n  for (i = 1; i &amp;lt; n; i++) {\n    next = arr[i];\n \n    if (s.isEmpty() == false) {\n      element = s.pop();\n \n      while (element &amp;lt; next) {\n        System.out.println(element + \" --> \" +\n          next);\n        if (s.isEmpty() == true)\n          break;\n        element = s.pop();\n      }\n \n      if (element > next)\n        s.push(element);\n    }\n \n    s.push(next);\n  }\n  while (s.isEmpty() == false) {\n    element = s.pop();\n    next = -1;\n    System.out.println(element + \" -- \" + next);\n  }\n}&lt;\/pre>\n&lt;!-- \/wp:enlighter\/codeblock --><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-black-background-color has-black-color is-style-wide\"\/>\n\n\n\n<h2 id=\"practice-question\">Practice Question<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.interviewbit.com\/problems\/nextgreater\/\" target=\"_blank\" rel=\"noreferrer noopener\">Next Greater Problem<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-black-background-color has-black-color is-style-wide\"\/>\n\n\n\n<h2 id=\"faqs\">FAQs<\/h2>\n\n\n\n<p><strong>What other data structure can be used other than stack?<br><\/strong>A queue can also be used to find the <strong>Next Greater Element.<\/strong><br><\/p>\n\n\n\n<p><strong>What is the worst case and best case scenario in the brute force approach?<br><\/strong>The best case is when the array is sorted in increasing order. This leads to an O(N) approach.<br>The worst case is when the array is sorted in decreasing order.<\/p>\n","protected":false},"excerpt":{"rendered":"Problem Statement Given an array A[], find the next greater elementG[i] for every element A[i] in the array.The&hellip;\n","protected":false},"author":5,"featured_media":3193,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_daextam_enable_autolinks":"","csco_singular_sidebar":"","csco_page_header_type":"","csco_appearance_grid":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0},"categories":[145],"tags":[457,485],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/2893"}],"collection":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/comments?post=2893"}],"version-history":[{"count":4,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/2893\/revisions"}],"predecessor-version":[{"id":3195,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/2893\/revisions\/3195"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media\/3193"}],"wp:attachment":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media?parent=2893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/categories?post=2893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/tags?post=2893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}