{"id":34238,"date":"2024-11-01T09:25:53","date_gmt":"2024-11-01T09:25:53","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34238"},"modified":"2024-11-01T10:58:03","modified_gmt":"2024-11-01T10:58:03","slug":"c-coding-test-course-finding-the-order-of-permutations-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34238\/","title":{"rendered":"C++ coding test course, finding the order of permutations"},"content":{"rendered":"<p><body><\/p>\n<p>Hello, everyone! Today, we will take a deep dive into the <strong>Finding the Order of Permutations<\/strong> problem, which frequently appears in C++ coding tests. This problem will be very helpful in understanding the concept of permutations and developing combinatorial problem-solving skills. Permutations refer to all possible arrangements of a given set of numbers, and problems utilizing this concept are applied in various fields.<\/p>\n<h2>Problem Description<\/h2>\n<p>We will explore the concept of permutations and C++ implementation through the process of solving a problem as follows:<\/p>\n<p><strong>Problem:<\/strong> Given integers N and K, find the K-th permutation among all permutations that can be made using the numbers from 1 to N. Assume K starts from 1.<\/p>\n<h3>Input Format<\/h3>\n<ul>\n<li>The first line contains integers N (1 \u2264 N \u2264 10) and K (1 \u2264 K \u2264 N!).<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<p>Output the <strong>K-th permutation<\/strong> on a single line, separated by spaces.<\/p>\n<h3>Example<\/h3>\n<p><strong>Input:<\/strong><br \/>\n    3 2<\/p>\n<p><strong>Output:<\/strong><br \/>\n    2 1 3<\/p>\n<h2>Approach to the Problem<\/h2>\n<p>To solve this problem, we can approach it with the following steps:<\/p>\n<ol>\n<li>Understand the basic concept of permutations.<\/li>\n<li>Design a method to find the K-th permutation when N numbers are given.<\/li>\n<li>Implement the method of generating permutations in code.<\/li>\n<li>Return the K-th permutation.<\/li>\n<\/ol>\n<h3>Method of Generating Permutations<\/h3>\n<p>There are various methods for generating permutations; here we will use the method utilizing <strong>factorials<\/strong>. The permutations of N numbers yield N! combinations, and the algorithm for generating these combinations helps to efficiently calculate the K-th permutation directly.<\/p>\n<h4>Factorial Calculation<\/h4>\n<p>The factorial N! is defined as N * (N-1) * (N-2) * &#8230; * 1. For example, 3! = 6. Through this, we can calculate the number of possible choices for selecting each number given N.<\/p>\n<h4>A Little More Specifically<\/h4>\n<p>When listing the numbers from 1 to N, the first number can be chosen based on K. For instance, if K is 2, we find the K-th permutation by considering the number of ways to choose the next number based on each chosen first number.<\/p>\n<h2>C++ Code Implementation<\/h2>\n<p>Now, let&#8217;s write the C++ code based on the above approach. This code will output the K-th permutation using the given N and K.<\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n\nusing namespace std;\n\n\/\/ Factorial calculation function\nlong long factorial(int n) {\n    if (n == 0) return 1;\n    long long result = 1;\n    for (int i = 1; i &lt;= n; i++) {\n        result *= i;\n    }\n    return result;\n}\n\n\/\/ Function to find the K-th permutation\nvector&lt;int&gt; getKthPermutation(int n, int k) {\n    vector&lt;int&gt; numbers;\n    for (int i = 1; i &lt;= n; i++) {\n        numbers.push_back(i);\n    }\n    \n    k--; \/\/ 0-indexing\n    vector&lt;int&gt; permutation;\n\n    for (int i = 0; i &lt; n; i++) {\n        long long fact = factorial(n - 1 - i);\n        int index = k \/ fact;\n        permutation.push_back(numbers[index]);\n        numbers.erase(numbers.begin() + index); \/\/ Remove the selected number from the list\n        k %= fact;\n    }\n\n    return permutation;\n}\n\nint main() {\n    int n, k;\n    cin &gt;&gt; n &gt;&gt; k;\n\n    vector&lt;int&gt; kthPermutation = getKthPermutation(n, k);\n    \n    for (int num : kthPermutation) {\n        cout &lt;&lt; num &lt;&lt; \" \";\n    }\n    \n    return 0;\n}\n    <\/code><\/pre>\n<h3>Code Explanation<\/h3>\n<ol>\n<li><strong>Factorial Function:<\/strong> Calculates and returns the factorial of the given N.<\/li>\n<li><strong>K-th Permutation Function:<\/strong> Stores numbers from 1 to N in a vector and iterates to find the K-th permutation.<\/li>\n<li>Calculates the number of possible scenarios at each step to decide which number to select.<\/li>\n<li>The selected number is removed from the list, and K is updated for the next number selection.<\/li>\n<\/ol>\n<h2>Testing and Result Verification<\/h2>\n<p>In the above code, input N and K, and check if it outputs the K-th permutation correctly. For example, entering N=3 and K=2 should output 2 1 3.<\/p>\n<h2>Conclusion<\/h2>\n<p>Today, we learned about how to find the order of permutations using C++. This algorithm can enhance understanding of permutations and help solve various algorithmic problems. Since it is a commonly tested type in coding interviews, make sure to practice sufficiently.<\/p>\n<p>Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, everyone! Today, we will take a deep dive into the Finding the Order of Permutations problem, which frequently appears in C++ coding tests. This problem will be very helpful in understanding the concept of permutations and developing combinatorial problem-solving skills. Permutations refer to all possible arrangements of a given set of numbers, and problems &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34238\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ coding test course, finding the order of permutations&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[111],"tags":[],"class_list":["post-34238","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ coding test course, finding the order of permutations - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/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:\/\/atmokpo.com\/w\/34238\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ coding test course, finding the order of permutations - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello, everyone! Today, we will take a deep dive into the Finding the Order of Permutations problem, which frequently appears in C++ coding tests. This problem will be very helpful in understanding the concept of permutations and developing combinatorial problem-solving skills. Permutations refer to all possible arrangements of a given set of numbers, and problems &hellip; \ub354 \ubcf4\uae30 &quot;C++ coding test course, finding the order of permutations&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34238\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:03+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34238\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34238\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ coding test course, finding the order of permutations\",\"datePublished\":\"2024-11-01T09:25:53+00:00\",\"dateModified\":\"2024-11-01T10:58:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34238\/\"},\"wordCount\":478,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34238\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34238\/\",\"name\":\"C++ coding test course, finding the order of permutations - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:53+00:00\",\"dateModified\":\"2024-11-01T10:58:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34238\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34238\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34238\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ coding test course, finding the order of permutations\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ coding test course, finding the order of permutations - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/34238\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ coding test course, finding the order of permutations - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello, everyone! Today, we will take a deep dive into the Finding the Order of Permutations problem, which frequently appears in C++ coding tests. This problem will be very helpful in understanding the concept of permutations and developing combinatorial problem-solving skills. Permutations refer to all possible arrangements of a given set of numbers, and problems &hellip; \ub354 \ubcf4\uae30 \"C++ coding test course, finding the order of permutations\"","og_url":"https:\/\/atmokpo.com\/w\/34238\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:53+00:00","article_modified_time":"2024-11-01T10:58:03+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34238\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34238\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ coding test course, finding the order of permutations","datePublished":"2024-11-01T09:25:53+00:00","dateModified":"2024-11-01T10:58:03+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34238\/"},"wordCount":478,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34238\/","url":"https:\/\/atmokpo.com\/w\/34238\/","name":"C++ coding test course, finding the order of permutations - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:53+00:00","dateModified":"2024-11-01T10:58:03+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34238\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34238\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34238\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ coding test course, finding the order of permutations"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34238"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34238\/revisions"}],"predecessor-version":[{"id":34239,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34238\/revisions\/34239"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}