{"id":20127,"date":"2022-11-08T20:03:06","date_gmt":"2022-11-08T14:33:06","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/"},"modified":"2022-11-08T20:03:06","modified_gmt":"2022-11-08T14:33:06","slug":"solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/","title":{"rendered":"[Solved] Print all permutation of length k that can be formed from a set of n characters?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-41487453\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"41487453\" data-parentid=\"41486059\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The problem is actually like counting in base <em>sizeof(set)<\/em> on length <em>k<\/em> (assuming the set has 10 items maximum).<\/p>\n<p>For instance, with a set of <code>{ 0, 1, 2 }<\/code> on length 2, you count from <code>00<\/code> to <code>22<\/code>, base 3.<\/p>\n<p>To solve the &#8220;one digit change only&#8221; constraint, instead of counting increasingly, do that only until the next 10<sup>th<\/sup> change. Then count decreasingly, then again increasingly etc&#8230;<\/p>\n<p>For instance in the example above<\/p>\n<pre><code>00 -&gt; 02 then increase the next tenth (12), then count downward\n12 -&gt; 10 then again +10 to get 20, then go up again\n20 -&gt; 22\n<\/code><\/pre>\n<p>On length 3, keep the same reasoning, change the next 10<sup>th<\/sup> then go up or down depending on the initial value of the current digit <\/p>\n<pre><code>000 -&gt; 002, 012 -&gt; 010, 020 -&gt; 022\n122 -&gt; 120, 110 -&gt; 112, 102 -&gt; 100\n200 -&gt; 202, 212 -&gt; 210, 220 -&gt; 222\n<\/code><\/pre>\n<p>A recursive algorithm is one approach. The function depth 0 takes care of the first (left) digit, i.e. the highest 10<sup>th<\/sup>, and count up or down depending on its current digit state. If <code>0<\/code>, count up, and down otherwise. For each state, before incrementing, the function calls itself recursively with the next (right) digit status (which is either <code>0<\/code> or the last item in the set). The maximal depth being the length <em>k<\/em>.<\/p>\n<p>Keep the digits status in an array of length <em>k<\/em>. Array is initialized to <code>{0 ... 0}<\/code>. Give the function the index in the array (starting with 0). For each iteration, if we&#8217;re at max depth (ie <code>i == k-1<\/code>), print the array ; otherwise call recursively the function with <code>i+1<\/code>.<\/p>\n<p>Pseudo code<\/p>\n<pre><code>k length of number (number of digits)\nN size of set (1 .. 10), values from 0 to N-1\nA array of size k\n\nA[0 .. k-1] = 0\n\nfunction f ( i )\nbegin\n   inc = -1 if (A[i] &gt; 0), 1 otherwise     # Increment\n   end =  0 if (A[i] &gt; 0), N-1 otherwise   # Max value\n   j is the counter\n\n   j = A[ i ]   # Init\n   while ( (inc&lt;0 AND j&gt;=end) OR (inc&gt;0 AND j&lt;=end) )\n   do\n        A[ i ] = j\n\n        if (i &lt; k-1) call f ( i+1 )\n        otherwise print array A\n\n        j = j + inc\n   done\nend\n\ncall f ( 0 )\n<\/code><\/pre>\n<p>This what you should get for <code>N = 3, and k = 4<\/code><\/p>\n<pre><code>0000 0001 0002 0012 0011 0010 0020 0021 0022\n0122 0121 0120 0110 0111 0112 0102 0101 0100\n0200 0201 0202 0212 0211 0210 0220 0221 0222\n1222 1221 1220 1210 1211 1212 1202 1201 1200\n1100 1101 1102 1112 1111 1110 1120 1121 1122\n1022 1021 1020 1010 1011 1012 1002 1001 1000\n2000 2001 2002 2012 2011 2010 2020 2021 2022\n2122 2121 2120 2110 2111 2112 2102 2101 2100\n2200 2201 2202 2212 2211 2210 2220 2221 2222\n<\/code><\/pre>\n<p>Note that you should always get  N<sup>k<\/sup> numbers&#8230;<\/p>\n<hr>\n<p>This is the C code that generated the above:<\/p>\n<pre><code>int a[20] = {0}; \/\/ Put here the right size instead of 20, or use #define...\nint N,k;\n\nvoid f(int i) {\n    int inc = a[i] ? -1:1;\n    int end = a[i] ? 0:N-1;\n    int j;\n\n    for(j=a[i] ; inc&lt;0 &amp;&amp; j&gt;=end || inc&gt;0 &amp;&amp; j&lt;=end ; j+=inc) {\n        a[i] = j;\n        if (i &lt; k-1) f(i+1);\n        else {\n            int z;\n            for(z=0 ; z&lt;k ; z++) printf(\"%d\", a[z]);\n            printf(\"\\n\");\n        }\n    }\n}\n<\/code><\/pre>\n<p>in <code>main()<\/code> initialize <code>N<\/code> and <code>k<\/code> and call<\/p>\n<pre><code>f(0);\n<\/code><\/pre>\n<hr>\n<p>An iterative version, that does basically the same thing<\/p>\n<pre><code>void fi() {\n    int z,i,inc[k];\n\n    for(i=0 ; i&lt;k ; i++) {\n      a[i] = 0;     \/\/ initialize our array if needed\n      inc[i] = 1;   \/\/ all digits are in +1 mode\n    }\n    int p = k-1;    \/\/ p, position: start from last digit (right)\n\n    while(p &gt;= 0) {\n        if (p == k-1) {\n            for(z=0 ; z&lt;k ; z++) printf(\"%d\", a[z]);\n            printf(\"\\n\");\n        }\n        if (inc[p]&lt;0 &amp;&amp; a[p]&gt;0 || inc[p]&gt;0 &amp;&amp; a[p]&lt;N-1) {\n          a[p] += inc[p];\n          p = k-1;\n        }\n        else {\n          inc[p] = -inc[p];\n          p--;\n        }\n    }\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">8<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Print all permutation of length k that can be formed from a set of n characters? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The problem is actually like counting in base sizeof(set) on length k (assuming the set has 10 items maximum). For instance, with a set of { 0, 1, 2 } on length 2, you count from 00 to 22, base 3. To solve the &#8220;one digit change only&#8221; constraint, instead of counting increasingly, do &#8230; <a title=\"[Solved] Print all permutation of length k that can be formed from a set of n characters?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/\" aria-label=\"More on [Solved] Print all permutation of length k that can be formed from a set of n characters?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[457,324,2000,323,362],"class_list":["post-20127","post","type-post","status-publish","format-standard","hentry","category-solved","tag-algorithm","tag-c","tag-combinatorics","tag-java","tag-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Print all permutation of length k that can be formed from a set of n characters? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Print all permutation of length k that can be formed from a set of n characters? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The problem is actually like counting in base sizeof(set) on length k (assuming the set has 10 items maximum). For instance, with a set of { 0, 1, 2 } on length 2, you count from 00 to 22, base 3. To solve the &#8220;one digit change only&#8221; constraint, instead of counting increasingly, do ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-08T14:33:06+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Print all permutation of length k that can be formed from a set of n characters?\",\"datePublished\":\"2022-11-08T14:33:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\\\/\"},\"wordCount\":278,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"algorithm\",\"c++\",\"combinatorics\",\"java\",\"string\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\\\/\",\"name\":\"[Solved] Print all permutation of length k that can be formed from a set of n characters? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-11-08T14:33:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Print all permutation of length k that can be formed from a set of n characters?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Print all permutation of length k that can be formed from a set of n characters? - JassWeb","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:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Print all permutation of length k that can be formed from a set of n characters? - JassWeb","og_description":"[ad_1] The problem is actually like counting in base sizeof(set) on length k (assuming the set has 10 items maximum). For instance, with a set of { 0, 1, 2 } on length 2, you count from 00 to 22, base 3. To solve the &#8220;one digit change only&#8221; constraint, instead of counting increasingly, do ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/","og_site_name":"JassWeb","article_published_time":"2022-11-08T14:33:06+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Print all permutation of length k that can be formed from a set of n characters?","datePublished":"2022-11-08T14:33:06+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/"},"wordCount":278,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["algorithm","c++","combinatorics","java","string"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/","url":"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/","name":"[Solved] Print all permutation of length k that can be formed from a set of n characters? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-08T14:33:06+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-print-all-permutation-of-length-k-that-can-be-formed-from-a-set-of-n-characters\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Print all permutation of length k that can be formed from a set of n characters?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=20127"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20127\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=20127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=20127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=20127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}