{"id":35112,"date":"2024-11-01T09:35:39","date_gmt":"2024-11-01T09:35:39","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35112"},"modified":"2024-11-01T11:44:57","modified_gmt":"2024-11-01T11:44:57","slug":"titlekotlin-coding-test-course-making-an-integer-1","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35112\/","title":{"rendered":"title>Kotlin Coding Test Course: Making an Integer 1<\/title"},"content":{"rendered":"<div class=\"post\">\n<p>Hello, everyone preparing for the coding test! Today, we will solve the problem of making an integer equal to 1 together. This problem is a great exercise for solidifying the basics of algorithms, and we will solve it using Kotlin.<\/p>\n<h2>Problem Description<\/h2>\n<p>The problem is to find the minimum number of operations needed to make the integer N equal to 1. The operations that can be used are as follows:<\/p>\n<ul>\n<li>If N is divisible by 3, divide N by 3.<\/li>\n<li>If N is divisible by 2, divide N by 2.<\/li>\n<li>Subtract 1.<\/li>\n<\/ul>\n<p>For example, when N is 10, the process of 10 -> 9 -> 3 -> 1 requires 3 operations.<\/p>\n<h2>Approach to the Problem<\/h2>\n<p>To solve this problem, we will use the dynamic programming (DP) technique. DP is a method of solving problems by breaking them down into smaller subproblems. In this case, we will create an array to store the minimum number of operations needed to make each number equal to 1.<\/p>\n<h3>Step-by-Step Approach<\/h3>\n<ol>\n<li><strong>Array Initialization<\/strong>: Create an array of size equal to the integer N and initialize each index with a default value of -1.<\/li>\n<li><strong>Base Case Setup<\/strong>: Store 0 at index 1 of the array. This indicates that no operations are needed to make it equal to 1.<\/li>\n<li><strong>DP Table Update through a Loop<\/strong>: Use a loop from 2 to N to calculate the minimum number of operations for each number. At this point, we calculate the new values using division operations and subtraction, and store them in the array.<\/li>\n<li><strong>Output the Result<\/strong>: Finally, retrieve the calculated value for N from the main array and output it.<\/li>\n<\/ol>\n<h2>Kotlin Code<\/h2>\n<pre><code>\nfun minOperationsToOne(n: Int): Int {\n    \/\/ Initialize the dynamic programming table\n    val dp = IntArray(n + 1) { Int.MAX_VALUE }\n    dp[1] = 0 \/\/ 0 operations are needed to make 1 equal to 1\n\n    for (i in 2..n) {\n        \/\/ Subtract 1\n        dp[i] = dp[i - 1] + 1\n        \n        \/\/ Divide by 2\n        if (i % 2 == 0) {\n            dp[i] = minOf(dp[i], dp[i \/ 2] + 1)\n        }\n        \n        \/\/ Divide by 3\n        if (i % 3 == 0) {\n            dp[i] = minOf(dp[i], dp[i \/ 3] + 1)\n        }\n    }\n    return dp[n]\n}\n\nfun main() {\n    val n = 10 \/\/ Test input\n    println(\"Minimum number of operations to make the integer equal to 1: ${minOperationsToOne(n)}\")\n}\n    <\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>The above code calculates the minimum number of operations needed to make the given integer N equal to 1. The <strong>minOperationsToOne<\/strong> function takes an integer input and uses dynamic programming to find the minimum number of operations. It checks the conditions for each operation to update the possible minimum counts. In particular, dividing by 2 and 3 is checked with conditional statements to perform the operations only when appropriate.<\/p>\n<h2>Execution Result<\/h2>\n<p>When the above code is executed, it outputs the minimum number of operations required to make the integer equal to 1 for the given input N. For example, when N is 10, the output will be &#8216;Minimum number of operations to make the integer equal to 1: 3&#8217;.<\/p>\n<h2>Time Complexity Analysis<\/h2>\n<p>The time complexity of this algorithm is O(N). It scans the array once and performs constant-time operations for each operation, allowing for efficient calculations. The space complexity is O(N) because space is required to store the DP array.<\/p>\n<h2>Conclusion<\/h2>\n<p>Today, we dealt with the problem of making an integer equal to 1. By solving the problem using Kotlin, we were able to learn the basics of dynamic programming. Problems of this type are often presented during job preparation, so it is recommended to practice regularly. I will return with useful content in the next lecture!<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/kotlinlang.org\/\">Kotlin Official Documentation<\/a><\/li>\n<li><a href=\"https:\/\/algospot.com\/\">Various problem solving through Algospot<\/a><\/li>\n<li><a href=\"https:\/\/www.acmicpc.net\/\">Baekjoon Algorithm Problems<\/a><\/li>\n<\/ul>\n<p>Thank you!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello, everyone preparing for the coding test! Today, we will solve the problem of making an integer equal to 1 together. This problem is a great exercise for solidifying the basics of algorithms, and we will solve it using Kotlin. Problem Description The problem is to find the minimum number of operations needed to make &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35112\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;title>Kotlin Coding Test Course: Making an Integer 1<\/title\"<\/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":[106],"tags":[],"class_list":["post-35112","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>title&gt;Kotlin Coding Test Course: Making an Integer 1<\/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\/35112\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"title&gt;Kotlin Coding Test Course: Making an Integer 1\" \/>\n<meta property=\"og:description\" content=\"Hello, everyone preparing for the coding test! Today, we will solve the problem of making an integer equal to 1 together. This problem is a great exercise for solidifying the basics of algorithms, and we will solve it using Kotlin. Problem Description The problem is to find the minimum number of operations needed to make &hellip; \ub354 \ubcf4\uae30 &quot;title&gt;Kotlin Coding Test Course: Making an Integer 1\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35112\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:35:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:44:57+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\/35112\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35112\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"title>Kotlin Coding Test Course: Making an Integer 1\",\"datePublished\":\"2024-11-01T09:35:39+00:00\",\"dateModified\":\"2024-11-01T11:44:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35112\/\"},\"wordCount\":396,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35112\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35112\/\",\"name\":\"title>Kotlin Coding Test Course: Making an Integer 1\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:35:39+00:00\",\"dateModified\":\"2024-11-01T11:44:57+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35112\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35112\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35112\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"title>Kotlin Coding Test Course: Making an Integer 1\"}]},{\"@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":"title>Kotlin Coding Test Course: Making an Integer 1","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\/35112\/","og_locale":"ko_KR","og_type":"article","og_title":"title>Kotlin Coding Test Course: Making an Integer 1","og_description":"Hello, everyone preparing for the coding test! Today, we will solve the problem of making an integer equal to 1 together. This problem is a great exercise for solidifying the basics of algorithms, and we will solve it using Kotlin. Problem Description The problem is to find the minimum number of operations needed to make &hellip; \ub354 \ubcf4\uae30 \"title>Kotlin Coding Test Course: Making an Integer 1","og_url":"https:\/\/atmokpo.com\/w\/35112\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:35:39+00:00","article_modified_time":"2024-11-01T11:44:57+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\/35112\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35112\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"title>Kotlin Coding Test Course: Making an Integer 1","datePublished":"2024-11-01T09:35:39+00:00","dateModified":"2024-11-01T11:44:57+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35112\/"},"wordCount":396,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35112\/","url":"https:\/\/atmokpo.com\/w\/35112\/","name":"title>Kotlin Coding Test Course: Making an Integer 1","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:35:39+00:00","dateModified":"2024-11-01T11:44:57+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35112\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35112\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35112\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"title>Kotlin Coding Test Course: Making an Integer 1"}]},{"@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\/35112","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=35112"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35112\/revisions"}],"predecessor-version":[{"id":35113,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35112\/revisions\/35113"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}