{"id":10914,"date":"2022-12-06T08:35:07","date_gmt":"2022-12-06T08:35:07","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=10914"},"modified":"2023-04-17T04:53:26","modified_gmt":"2023-04-17T04:53:26","slug":"next-permutation-and-previous-permutation-in-cpp","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/","title":{"rendered":"Previous  Permutation and  Next Permutation in C++"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.jpg\" alt=\"\" \/><\/p>\n<p>Permutations are popular topics in mathematics and computer science. In computer science, permutations are used to solve a wide range of problems, such as generating all possible combinations of a set of elements, sorting algorithms, and generating test cases for software. In this article, we will learn the ways to find the next and previous, and next permutation in C++ of the given arrangement approaches and code implementation of the previous and next permutation in C++.<\/p>\n<h2>Previous and Next Permutation in C++<\/h2>\n<p>Given an array of numbers, rearrange the array such that the rearranged array becomes lexicographically next greater and previous smaller permutation of the initial given array.<\/p>\n<h3>Examples of Next Greater Permutation<\/h3>\n<ol>\n<li>[1,2,3] will have the next greater permutation [1,3,2].<\/li>\n<li>[1,3,2] will have the next greater permutation [2,1,3].<\/li>\n<li>[1,2,3,6,5,4] will have the next greater permutation [1,2,4,3,5,6].<\/li>\n<\/ol>\n<h3>Examples of Previous Smaller Permutation<\/h3>\n<ol>\n<li>[3,2,1] will have the previous smaller permutation [3,1,2].<\/li>\n<li>[3,1,2] will have the previous smaller permutation [2,3,1].<\/li>\n<li>[1,9,4,6,7] will have the previous smaller permutation [1,7,4,6,9].<\/li>\n<\/ol>\n<h2>Brute Force Approach for finding Previous and Next Permutations in C++<\/h2>\n<p>In this approach we will find all the possible arrangements of  the given array and then we will find the arrangement which is smallest but greater than given arrangement as Next Greater Permutation and largest but smaller than given arrangement as Previous Smaller Permutation using recursion.<\/p>\n<p><strong>Time Complexity of Previous and Next Permutation in C++: O(N!XN)<\/strong><\/p>\n<p>Where N is the number of elements in the given array.<br \/>\nMaking the total number will require time complexity O(N!XN) and there will be a total N! Arrangement possible with the N number of elements so searching time will be O(N!).<\/p>\n<p><strong>Space Complexity of  Previous and Next Permutation in C++: O(1).<\/strong><\/p>\n<p>We don\u2019t require any additional space for this.<\/p>\n<h2>Next Permutation in C++ Using Inbuilt Function<\/h2>\n<p>We can use the inbuilt function in C++ called next_permutation() which will directly return the lexicographically next greater permutation in C++.<\/p>\n<h3>Code for Next Permutation in C++ Using Inbuilt Function<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_10911 {\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_10911 .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_10911 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_10911 .wpsm_nav-tabs > li.active > a, #tab_container_10911 .wpsm_nav-tabs > li.active > a:hover, #tab_container_10911 .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_10911 .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_10911 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_10911 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_10911 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_10911 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_10911 .wpsm_nav-tabs > li > a:hover , #tab_container_10911 .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_10911 .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_10911 .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_10911 .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_10911 .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_10911 .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_10911 .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_10911 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10911 .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_10911 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10911 .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_10911 .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_10911\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_10911\">\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_10911_1\" aria-controls=\"tabs_desc_10911_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\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_10911\">\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_10911_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=\"droide\" 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\nint main() {\r\n    int arr[] = {1,3,2};    \r\n    next_permutation(arr,arr+3);\r\n\/\/using in-built function of C++    \r\n    cout<<arr[0]<<\" \"<<arr[1]<<\" \"<<arr[2];    \r\n    return 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\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_10911 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_10911 a\"),jQuery(\"#tab-content_10911\"));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<h2>Solution 3 for Next Permutation in C++<\/h2>\n<p><strong>Intuition for next permutation in C++<\/strong><\/p>\n<p>Observe the following case:<br \/>\nGiven Array             Next Greater Permutation<br \/>\n1, 2, 3                 1, 3, 2<br \/>\n2, 1, 3                 2, 3, 1<br \/>\n1, 3, 2                 2, 1, 3<br \/>\n3, 2, 1                 Not Possible<\/p>\n<p>We can observe that Next permutation is possible unless the arrangement is sorted in descending order.<br \/>\nAlso we can see that after a certain index the Next Permutation is in sorted index in ascending order.<br \/>\nWe have to find the largest index i such that a[i] &lt; a[i+1].Here a[i+1] is the starting point from which an increasing sequence starts  until the end of the array.<br \/>\nAfter finding the index i, we will swap the value at i index with a value which is smallest but greater than a[i] which is present at index j where i &lt; j&lt; size(array).<\/p>\n<h3>Approach to Find the Next Permutation in C++<\/h3>\n<ol>\n<li>Linearly traverse through the array from the backwards and find the index i such that a[i] &lt; a[i+1].<\/li>\n<li>If i value is less than zero, return the given array in reverse order, as there is no lexicographically next greater permutation is possible as the array is sorted in descending order. So we return the lexicographically smallest permutation which will be the reverse of the given arrangement.<\/li>\n<li>Else find the index j such that j&gt;i and a[j] is the smallest number greater than a[i] in i+1 to size(array)-1.<\/li>\n<li>Swap the values between index i and index j.<\/li>\n<li>Reverse the array from index (i+1) till the end of the array.<\/li>\n<\/ol>\n<h3>Code for Finding the Next Permutation in C++<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_10912 {\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_10912 .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_10912 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_10912 .wpsm_nav-tabs > li.active > a, #tab_container_10912 .wpsm_nav-tabs > li.active > a:hover, #tab_container_10912 .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_10912 .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_10912 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_10912 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_10912 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_10912 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_10912 .wpsm_nav-tabs > li > a:hover , #tab_container_10912 .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_10912 .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_10912 .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_10912 .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_10912 .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_10912 .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_10912 .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_10912 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10912 .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_10912 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10912 .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_10912 .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_10912\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_10912\">\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_10912_1\" aria-controls=\"tabs_desc_10912_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\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_10912\">\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_10912_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\/\/ Function next_permutation will find the next permutation in C++\r\n\tvoid next_permutation(vector&lt;int&gt;&amp; v) {\r\n    \tint i=v.size()-2;\r\n    \twhile(i&gt;=0){\r\n        \tif(v[i]&lt;v[i+1]){\r\n            \tbreak;\r\n        \t}\r\n        \ti--;\r\n    \t}\r\n    \tif(i&lt;0){\r\n        \treverse(v.begin(),v.end());\r\n        \treturn;\r\n    \t}\r\n    \tint j=v.size()-1;\r\n    \twhile(j&gt;i){\r\n        \tif(v[j]&gt;v[i]){\r\n            \tbreak;\r\n        \t}\r\n        \tj--;\r\n    \t}\r\n    \tint x=v[i];\r\n    \tv[i]=v[j];\r\n    \tv[j]=x;\r\n    \treverse(v.begin()+i+1,v.end());\r\n    \treturn;\r\n\t}\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_10912 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_10912 a\"),jQuery(\"#tab-content_10912\"));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- O(N)<\/strong><\/p>\n<p>Where N is the number of elements present in the array.<br \/>\nFor 1st backward iteration, Time Complexity O(N).<br \/>\nFor 2nd backward iteration, Time Complexity O(N).<br \/>\nFor swapping, Time Complexity O(1).<br \/>\nFor reversing, Time complexity O(N).<br \/>\nTo sum up overall Time Complexity is 3*O(N), which approximately becomes O(N).<\/p>\n<p><strong>Space Complexity- O(1)<\/strong><\/p>\n<p>No extra space is required. Thus its space complexity is O(1).<\/p>\n<h3>Dry Run for Next Greater Permutation in C++<\/h3>\n<p>Consider the input array [1, 3, 2].<\/p>\n<ol>\n<li>Traversing the array backward linearly and stop at the point where  a[i] &lt; a[i+1]. Initially i=sizeof array-2(i=1 in this case).<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644676-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B%201.png\" alt=\"\" \/><\/p>\n<ol start=\"2\">\n<li>Since i=1 and a[i]=3 and a[i+1]=2 and 3&gt;2, decrease i by 1(i=0).<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644676-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B%202.png\" alt=\"\" \/><\/p>\n<ol start=\"3\">\n<li>Now i=0, a[i]=1 and a[i+1]=3 and 1&lt; 3, which satisfies the condition, so break.<\/li>\n<li>Now, we have to find index j where j&gt;i and a[j] is the smallest number greater than a[i]. We will traverse backward linearly again, Initially j=sizeof array -1 (j=2 in  this case).<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644677-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B%203.png\" alt=\"\" \/><\/p>\n<ol start=\"5\">\n<li>j=2, and a[j]&gt;a[i], so we have found the index value of j, so break.<\/li>\n<li>Swap the value at index i and index j.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644677-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B%204.png\" alt=\"\" \/><\/p>\n<ol start=\"7\">\n<li>Reverse the array from index i+1.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644677-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B%205.png\" alt=\"\" \/><\/p>\n<p>This is the Next Greater Permutation of the given array.<\/p>\n<h2>Optimal Solution for Previous Smaller Permutation in C++<\/h2>\n<p><strong>Intuition<\/strong><br \/>\nObserve the following case:<br \/>\nGiven Array             Previous Smaller Permutation<br \/>\n1, 2, 3                 Not Possible<br \/>\n2, 1, 3                 1, 3, 2<br \/>\n1, 3, 2                 1, 2, 3<br \/>\n3, 2, 1                 3, 1, 2<\/p>\n<p>We can observe that Previous Smaller Permutation is only    possible if and only if the array is not ascendingly sorted. <\/p>\n<p>We have to find the index i where a[i]&gt;a[i+1]. Then find an index j such that i &lt; j &lt; size of array and a[j] is the largest number which is smaller than a[i]. And if the largest number is repeated more than once, take the index value j greater than i but as small as possible.<\/p>\n<h3>Approach to Find Previous Smaller Permutation<\/h3>\n<ol>\n<li>Linearly traverse through the array from the backwards and find the index i such that a[i]&gt;a[i+1].<\/li>\n<li>If index i is less than zero, then the given arrangement is the smallest arrangement possible (arranged in ascending order), so return the array as it is.<\/li>\n<li>Else find the index j such that j&gt;i and a[j] is the largest value smaller than a[i]. If the largest number smaller than a[i] is repeated more than one then j should be as small as possible.<\/li>\n<li>Swap the values of index i and index j.<\/li>\n<\/ol>\n<h3>Code for finding the Previous Smaller Permutation in C++<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_10913 {\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_10913 .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_10913 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_10913 .wpsm_nav-tabs > li.active > a, #tab_container_10913 .wpsm_nav-tabs > li.active > a:hover, #tab_container_10913 .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_10913 .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_10913 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_10913 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_10913 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_10913 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_10913 .wpsm_nav-tabs > li > a:hover , #tab_container_10913 .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_10913 .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_10913 .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_10913 .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_10913 .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_10913 .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_10913 .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_10913 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10913 .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_10913 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10913 .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_10913 .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_10913\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_10913\">\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_10913_1\" aria-controls=\"tabs_desc_10913_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\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_10913\">\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_10913_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\nvector&lt;int&gt; prev_permutation(vector&lt;int&gt;&amp; a) {\r\n    \tint i=a.size()-2;\r\n    \twhile(i&gt;=0){\r\n        \tif(a[i]&gt;a[i+1]){\r\n            \tbreak;\r\n        \t}\r\n        \ti--;\r\n    \t}\r\n    \tif(i&lt;0){\r\n        \treturn a;\r\n    \t}\r\n    \tint j=a.size()-1,mx=INT_MIN;\r\n    \tint y=a.size()-1;\r\n    \twhile(j&gt;i){\r\n        \tif(a[i]&gt;a[j]){\r\n            \tif(mx&lt;=a[j]){\r\n                \ty=j;\r\n                \tmx=a[j];\r\n            \t}\r\n        \t}\r\n        \tj--;\r\n    \t}\r\n    \tint x=a[i];\r\n    \ta[i]=a[y];\r\n    \ta[y]=x;\r\n    \treturn a;\r\n\t}\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_10913 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_10913 a\"),jQuery(\"#tab-content_10913\"));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- O(N)<\/strong><\/p>\n<p>Where N is the number of elements present in the array.<br \/>\nFor 1st backward iteration, Time Complexity O(N).<br \/>\nFor 2nd backward iteration, Time Complexity O(N).<br \/>\nFor swapping, Time Complexity O(1).<br \/>\nTo sum up overall Time Complexity is 2*O(N), which approximately becomes O(N).<\/p>\n<p><strong>Space Complexity- O(1)<\/strong><\/p>\n<p>No extra space is required. Thus its space complexity is O(1).<\/p>\n<h3>Dry Run for Previous Smaller Permutation in C++<\/h3>\n<p>Consider the input array [1,3, 2].<\/p>\n<ol>\n<li>Iterate the array backward and stop at the point where a[i]&gt;a[i+1]. Initially i=sizeof array-2 (i=1 in this case).<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644677-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B%206.png\" alt=\"\" \/><\/p>\n<ol start=\"2\">\n<li>\n<p>i=1, a[i]=3 and a[i]&gt;a[i+1], so we have found the required i index, so break the loop.<\/p>\n<\/li>\n<li>\n<p>Now, we have to find index j such that j&gt;i and a[j] has the largest value smaller than a[i]. In case of more than one index containing this value choose the index j as small as possible. We will traverse backward linearly again, Initially j=sizeof array -1 (j=2 in  this case).Break the loop when j-1=i.<\/p>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644677-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B%207.png\" alt=\"\" \/><\/p>\n<ol start=\"4\">\n<li>\n<p>j=2 and a[j] &lt; a[i], here j-1=i so break the loop.<\/p>\n<\/li>\n<li>\n<p>Swap the values of index i and index j.<\/p>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644678-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B%208.png\" alt=\"\" \/><\/p>\n<p>This the previous permutation of given array.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nNow we can conclude the previous and next permutation in C++ with different approaches, Hope all approaches are clear and efficient to find the previous and next permutation in C++ The algorithms discussed in this article are efficient and widely used in practice. By using these algorithms, you can solve a wide range of problems that involve permutations in C++.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<p><strong>Q1. What is the next permutation in C++?<\/strong><br \/>\n<strong>Ans.<\/strong> The next permutation in C++ is a function from the C++ standard library that rearranges the elements of a container such that they form the lexicographically next permutation of the original container.<\/p>\n<p><strong>Q2. How is the next permutation implemented in C++?<\/strong><br \/>\n<strong>Ans.<\/strong> The next permutation in C++ can be implemented in C++ using the std::next_permutation() function from the algorithm header.<\/p>\n<p><strong>Q3. Can these algorithms be used to generate permutations of non-integer types?<\/strong><br \/>\n<strong>Ans.<\/strong> Yes, these algorithms can be used to generate permutations of any type that supports the less-than operator (&lt;) and the swap function. For example, you can use these algorithms to generate permutations of strings, characters, or custom data types.<\/p>\n<p><strong>Q4. What happens if the sequence is already the largest or smallest permutation?<\/strong><br \/>\n<strong>Ans.<\/strong> If the sequence is already the largest permutation, then the next_permutation algorithm will generate the smallest permutation. If the sequence is already the smallest permutation, then the prev_permutation algorithm will generate the largest permutation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Permutations are popular topics in mathematics and computer science. In computer science, permutations are used to solve a wide range of problems, such as generating all possible combinations of a set of elements, sorting algorithms, and generating test cases for software. In this article, we will learn the ways to find the next and previous, [&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":[3],"tags":[],"class_list":["post-10914","post","type-post","status-publish","format-standard","hentry","category-cpp-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Next Permutation and Previous Permutation in C++<\/title>\n<meta name=\"description\" content=\"We will learn the ways to find the next and previous permutation of the given array of Numbers in C++.\" \/>\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\/next-permutation-and-previous-permutation-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Next Permutation and Previous Permutation in C++\" \/>\n<meta property=\"og:description\" content=\"We will learn the ways to find the next and previous permutation of the given array of Numbers in C++.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/\" \/>\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-12-06T08:35:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-17T04:53:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.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=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Previous Permutation and Next Permutation in C++\",\"datePublished\":\"2022-12-06T08:35:07+00:00\",\"dateModified\":\"2023-04-17T04:53:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/\"},\"wordCount\":1481,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.jpg\",\"articleSection\":[\"C++ Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/\",\"name\":\"Next Permutation and Previous Permutation in C++\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.jpg\",\"datePublished\":\"2022-12-06T08:35:07+00:00\",\"dateModified\":\"2023-04-17T04:53:26+00:00\",\"description\":\"We will learn the ways to find the next and previous permutation of the given array of Numbers in C++.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Programming\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/cpp-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Previous Permutation and Next Permutation in C++\"}]},{\"@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":"Next Permutation and Previous Permutation in C++","description":"We will learn the ways to find the next and previous permutation of the given array of Numbers in C++.","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\/next-permutation-and-previous-permutation-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Next Permutation and Previous Permutation in C++","og_description":"We will learn the ways to find the next and previous permutation of the given array of Numbers in C++.","og_url":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-12-06T08:35:07+00:00","article_modified_time":"2023-04-17T04:53:26+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Previous Permutation and Next Permutation in C++","datePublished":"2022-12-06T08:35:07+00:00","dateModified":"2023-04-17T04:53:26+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/"},"wordCount":1481,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.jpg","articleSection":["C++ Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/","url":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/","name":"Next Permutation and Previous Permutation in C++","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.jpg","datePublished":"2022-12-06T08:35:07+00:00","dateModified":"2023-04-17T04:53:26+00:00","description":"We will learn the ways to find the next and previous permutation of the given array of Numbers in C++.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1670308644400-Next%20Permutation%20and%20Previous%20Permutation%20in%20C%2B%2B.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/next-permutation-and-previous-permutation-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"C++ Programming","item":"https:\/\/prepbytes.com\/blog\/category\/cpp-programming\/"},{"@type":"ListItem","position":3,"name":"Previous Permutation and Next Permutation in C++"}]},{"@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\/10914","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=10914"}],"version-history":[{"count":2,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/10914\/revisions"}],"predecessor-version":[{"id":15641,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/10914\/revisions\/15641"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=10914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=10914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=10914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}