{"id":35110,"date":"2024-11-01T09:35:38","date_gmt":"2024-11-01T09:35:38","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35110"},"modified":"2024-11-01T11:44:57","modified_gmt":"2024-11-01T11:44:57","slug":"kotlin-coding-test-course-implementing-absolute-value-heap","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35110\/","title":{"rendered":"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP"},"content":{"rendered":"<article>\n<p>\n        Kotlin is a programming language that has recently gained popularity among many developers due to its modern and concise syntax as well as its powerful features. In coding tests, specifically algorithm problem-solving, Kotlin demonstrates its usefulness as well. In this post, I would like to explain in detail how to implement an Absolute Value Heap using Kotlin.\n    <\/p>\n<h2>Problem Description<\/h2>\n<p>\n        The definition of an absolute value heap is as follows:\n    <\/p>\n<blockquote><p>\n        An absolute value heap is a priority queue that always places the smallest absolute value at the top.<br \/>\n        If there are multiple values with the same absolute value, the actual smaller value is prioritized.\n    <\/p><\/blockquote>\n<p>\n        The operations of the heap are as follows:<\/p>\n<ul>\n<li><strong>INSERT X<\/strong>: Adds integer X (to the priority queue)<\/li>\n<li><strong>EXTRACT<\/strong>: Deletes and outputs the integer with the smallest absolute value<\/li>\n<\/ul>\n<h2>Input Format<\/h2>\n<p>\n        The program&#8217;s input is provided over multiple lines, each containing a command.<br \/>\n        Each line is given in the form of &#8216;INSERT X&#8217; or &#8216;EXTRACT&#8217;.\n    <\/p>\n<h2>Output Format<\/h2>\n<p>\n        Each time an &#8216;EXTRACT&#8217; command is called, the outputted value is printed on a new line.\n    <\/p>\n<h2>Example<\/h2>\n<pre><code>\n    INPUT:\n    INSERT -5\n    INSERT 3\n    INSERT -1\n    EXTRACT\n    EXTRACT\n    INSERT -2\n    EXTRACT\n    EXTRACT\n    EXTRACT\n    <\/code><\/pre>\n<p>\n        OUTPUT:<br \/>\n        -1<br \/>\n        -2<br \/>\n        3\n    <\/p>\n<h2>Problem Analysis<\/h2>\n<p>\n        To solve the given problem, we need to consider the following:<\/p>\n<ul>\n<li>How should we structure the priority queue?<\/li>\n<li>Should we utilize Java&#8217;s heap structure or Kotlin&#8217;s collections?<\/li>\n<li>How do we sort based on absolute values?<\/li>\n<\/ul>\n<p>\n        To create an absolute value heap, we can use a min-heap structure.<br \/>\n        In Java, we can easily implement this using <code>PriorityQueue<\/code>.<br \/>\n        We can also utilize this in Kotlin, and we can implement the desired functionality using a basic heap structure.\n    <\/p>\n<h2>Implementation Process<\/h2>\n<h3>1. Define Data Structure<\/h3>\n<p>\n        The most crucial aspect of the absolute value heap is how we store the data.<br \/>\n        Since we need to sort based on absolute values, we will use <code>PriorityQueue<\/code> to add and remove elements.<br \/>\n        When adding elements, it is advisable to set both the absolute value and the original value.\n    <\/p>\n<h3>2. Define Comparator<\/h3>\n<p>\n        To define priorities in the <code>PriorityQueue<\/code>, we define a custom sorting rule (Comparator).<br \/>\n        Here, values with smaller absolute values should come first, and in the case of equal absolute values, the actual smaller value should be prioritized.\n    <\/p>\n<pre><code>\n    val comparator = Comparator<pair<int, int=\"\">&gt; { a, b -&gt;\n        if (Math.abs(a.first) == Math.abs(b.first)) {\n            a.first.compareTo(b.first)\n        } else {\n            Math.abs(a.first).compareTo(Math.abs(b.first))\n        }\n    }\n    <\/pair<int,><\/code><\/pre>\n<h3>3. Process Commands<\/h3>\n<p>\n        To handle the input commands, we run a loop.<br \/>\n        If the command is <code>INSERT X<\/code>, we add the value to the heap, and if the command is <code>EXTRACT<\/code>, we remove the value from the heap and output it.\n    <\/p>\n<pre><code>\n    val priorityQueue = PriorityQueue<pair<int, int=\"\">&gt;(comparator)\n\n    val reader = BufferedReader(InputStreamReader(System.`in`))\n    val n = reader.readLine().toInt()\n    \n    repeat(n) {\n        val command = reader.readLine().split(\" \")\n        when (command[0]) {\n            \"INSERT\" -&gt; {\n                val value = command[1].toInt()\n                priorityQueue.add(Pair(value, value))\n            }\n            \"EXTRACT\" -&gt; {\n                if (priorityQueue.isNotEmpty()) {\n                    println(priorityQueue.poll().first)\n                } else {\n                    println(0)\n                }\n            }\n        }\n    }\n    <\/pair<int,><\/code><\/pre>\n<h2>Full Code<\/h2>\n<pre><code>\n    import java.io.BufferedReader\n    import java.io.InputStreamReader\n    import java.util.PriorityQueue\n\n    fun main() {\n        val comparator = Comparator<pair<int, int=\"\">&gt; { a, b -&gt;\n            if (Math.abs(a.first) == Math.abs(b.first)) {\n                a.first.compareTo(b.first)\n            } else {\n                Math.abs(a.first).compareTo(Math.abs(b.first))\n            }\n        }\n\n        val priorityQueue = PriorityQueue<pair<int, int=\"\">&gt;(comparator)\n\n        val reader = BufferedReader(InputStreamReader(System.`in`))\n        val n = reader.readLine().toInt()\n\n        repeat(n) {\n            val command = reader.readLine().split(\" \")\n            when (command[0]) {\n                \"INSERT\" -&gt; {\n                    val value = command[1].toInt()\n                    priorityQueue.add(Pair(value, value))\n                }\n                \"EXTRACT\" -&gt; {\n                    if (priorityQueue.isNotEmpty()) {\n                        println(priorityQueue.poll().first)\n                    } else {\n                        println(0)\n                    }\n                }\n            }\n        }\n    }\n    <\/pair<int,><\/pair<int,><\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>\n        Implementing an absolute value heap is an essential part of dynamic data structures, and it can assist in solving various problems.<br \/>\n        Through this tutorial, we learned how to implement a heap simply and effectively in Kotlin.<br \/>\n        I hope you were able to realize how important it is to understand the essence of a problem and choose an efficient data structure while solving algorithm problems.<br \/>\n        I look forward to improving our skills by tackling various algorithm problems together in the future!\n    <\/p>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>Kotlin is a programming language that has recently gained popularity among many developers due to its modern and concise syntax as well as its powerful features. In coding tests, specifically algorithm problem-solving, Kotlin demonstrates its usefulness as well. In this post, I would like to explain in detail how to implement an Absolute Value Heap &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35110\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP&#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":[106],"tags":[],"class_list":["post-35110","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>KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP - \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\/35110\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Kotlin is a programming language that has recently gained popularity among many developers due to its modern and concise syntax as well as its powerful features. In coding tests, specifically algorithm problem-solving, Kotlin demonstrates its usefulness as well. In this post, I would like to explain in detail how to implement an Absolute Value Heap &hellip; \ub354 \ubcf4\uae30 &quot;KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35110\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:35:38+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=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/35110\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35110\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP\",\"datePublished\":\"2024-11-01T09:35:38+00:00\",\"dateModified\":\"2024-11-01T11:44:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35110\/\"},\"wordCount\":477,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35110\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35110\/\",\"name\":\"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:35:38+00:00\",\"dateModified\":\"2024-11-01T11:44:57+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35110\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35110\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35110\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP\"}]},{\"@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":"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP - \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\/35110\/","og_locale":"ko_KR","og_type":"article","og_title":"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Kotlin is a programming language that has recently gained popularity among many developers due to its modern and concise syntax as well as its powerful features. In coding tests, specifically algorithm problem-solving, Kotlin demonstrates its usefulness as well. In this post, I would like to explain in detail how to implement an Absolute Value Heap &hellip; \ub354 \ubcf4\uae30 \"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP\"","og_url":"https:\/\/atmokpo.com\/w\/35110\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:35:38+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":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/35110\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35110\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP","datePublished":"2024-11-01T09:35:38+00:00","dateModified":"2024-11-01T11:44:57+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35110\/"},"wordCount":477,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35110\/","url":"https:\/\/atmokpo.com\/w\/35110\/","name":"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:35:38+00:00","dateModified":"2024-11-01T11:44:57+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35110\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35110\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35110\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"KOTLIN CODING TEST COURSE, IMPLEMENTING ABSOLUTE VALUE HEAP"}]},{"@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\/35110","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=35110"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35110\/revisions"}],"predecessor-version":[{"id":35111,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35110\/revisions\/35111"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}