{"id":33364,"date":"2024-11-01T09:15:54","date_gmt":"2024-11-01T09:15:54","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33364"},"modified":"2024-11-01T11:39:00","modified_gmt":"2024-11-01T11:39:00","slug":"java-coding-test-course-finding-the-kth-number-in-an-array","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33364\/","title":{"rendered":"Java Coding Test Course, Finding the Kth Number in an Array"},"content":{"rendered":"<p>In this lecture, we will address one of the commonly tested algorithm problems in Java, which is the &#8220;Finding the Kth Smallest Number in an Array&#8221; problem. This problem involves finding the Kth smallest number in a given array and can be approached in various ways. The ability to solve such types of problems is crucial for employment coding tests, so let\u2019s aim to upgrade our coding skills through this.<\/p>\n<h2>Problem Definition<\/h2>\n<pre>\nWrite a function that finds and returns the Kth smallest number from the given array.\n\nInput:\n- Integer array arr: an array of size n (1 \u2264 n \u2264 1000)\n- Integer K: K is a value between 1 and n inclusive\n\nOutput:\n- Return the Kth smallest number from the array\n<\/pre>\n<h2>Example<\/h2>\n<pre>\nFor example, if \narr = [7, 10, 4, 3, 20, 15] and K = 3, \nthe answer is 7.\n<\/pre>\n<h2>Approach<\/h2>\n<p>To solve this problem, several methods can be considered. The most basic approach is as follows.<\/p>\n<ul>\n<li><strong>Method Using Sorting<\/strong>: Sort the array and return the value at the K-1 index.<\/li>\n<li><strong>Selection Algorithm (Quickselect)<\/strong>: Use the Quickselect algorithm to directly find the Kth number.<\/li>\n<li><strong>Method Using Heap<\/strong>: Use a min-heap to find the Kth smallest value.<\/li>\n<\/ul>\n<h3>Method 1: Method Using Sorting<\/h3>\n<p>The most intuitive method is to sort the array and return the value at the K-1 index. Using one of the sorting algorithms, the time complexity is O(n log n).<\/p>\n<pre><code>public class KthSmallestNumber {\n    public static int findKthSmallest(int[] arr, int K) {\n        Arrays.sort(arr);\n        return arr[K - 1];\n    }\n    \n    public static void main(String[] args) {\n        int[] arr = {7, 10, 4, 3, 20, 15};\n        int K = 3;\n        System.out.println(\"Kth smallest number: \" + findKthSmallest(arr, K));  \/\/ 7\n    }\n}\n<\/code><\/pre>\n<h3>Method 2: Quickselect Algorithm<\/h3>\n<p>The Quickselect algorithm is a method to quickly find the Kth smallest element through a process similar to quicksort. This algorithm has an average performance of O(n).<\/p>\n<p>Quickselect chooses a pivot and partitions the array into smaller and larger values based on the pivot. It then recursively searches in the section where the Kth number should be located.<\/p>\n<pre><code>public class KthSmallestNumber {\n    public static int partition(int[] arr, int left, int right, int pivotIndex) {\n        int pivotValue = arr[pivotIndex];\n        swap(arr, pivotIndex, right);   \/\/ Move the pivot to the end of the array\n        int storeIndex = left;\n\n        for (int i = left; i &lt; right; i++) {\n            if (arr[i] &lt; pivotValue) {\n                swap(arr, storeIndex, i);\n                storeIndex++;\n            }\n        }\n        swap(arr, storeIndex, right);  \/\/ Move the pivot to its final place\n        return storeIndex;              \/\/ Return the final index of the pivot\n    }\n\n    public static void swap(int[] arr, int a, int b) {\n        int temp = arr[a];\n        arr[a] = arr[b];\n        arr[b] = temp;\n    }\n\n    public static int quickSelect(int[] arr, int left, int right, int K) {\n        if (left == right) {\n            return arr[left];  \/\/ Only one element left\n        }\n        \n        int pivotIndex = left + (right - left) \/ 2; \n        pivotIndex = partition(arr, left, right, pivotIndex);\n        \n        if (K == pivotIndex) {\n            return arr[K];\n        } else if (K &lt; pivotIndex) {\n            return quickSelect(arr, left, pivotIndex - 1, K);\n        } else {\n            return quickSelect(arr, pivotIndex + 1, right, K);\n        }\n    }\n\n    public static int findKthSmallest(int[] arr, int K) {\n        return quickSelect(arr, 0, arr.length - 1, K - 1);\n    }\n\n    public static void main(String[] args) {\n        int[] arr = {7, 10, 4, 3, 20, 15};\n        int K = 3;\n        System.out.println(\"Kth smallest number: \" + findKthSmallest(arr, K));  \/\/ 7\n    }\n}\n<\/code><\/pre>\n<h3>Method 3: Method Using Min-Heap<\/h3>\n<p>A min-heap has the characteristic that the smallest value is always at the root. When the size of the given array is n, we can extract the smallest K elements using a min-heap to find the Kth element. This method has a performance of O(n + k log n).<\/p>\n<pre><code>import java.util.PriorityQueue;\n\npublic class KthSmallestNumber {\n    public static int findKthSmallest(int[] arr, int K) {\n        PriorityQueue<Integer> minHeap = new PriorityQueue&lt;&gt;();\n        \n        for (int num : arr) {\n            minHeap.offer(num);\n        }\n        \n        int kthSmallest = -1;\n        for (int i = 0; i &lt; K; i++) {\n            kthSmallest = minHeap.poll();\n        }\n        \n        return kthSmallest;\n    }\n\n    public static void main(String[] args) {\n        int[] arr = {7, 10, 4, 3, 20, 15};\n        int K = 3;\n        System.out.println(\"Kth smallest number: \" + findKthSmallest(arr, K));  \/\/ 7\n    }\n}\n<\/integer><\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we explored various algorithmic approaches to the problem of &#8220;Finding the Kth Smallest Number in an Array.&#8221; We introduced three methods: a simple method using sorting, an efficient method using the Quickselect algorithm, and a method using a min-heap. To cultivate algorithmic problem-solving skills, it is important to experience a variety of problems and understand in which situations each method performs optimally.<\/p>\n<p>By regularly practicing such problems and building your problem-solving patterns and strategies, you can achieve good results in coding tests.<\/p>\n<p>I hope that through more lectures covering algorithm problems and their solutions, your coding skills will advance even further.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lecture, we will address one of the commonly tested algorithm problems in Java, which is the &#8220;Finding the Kth Smallest Number in an Array&#8221; problem. This problem involves finding the Kth smallest number in a given array and can be approached in various ways. The ability to solve such types of problems is &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33364\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, Finding the Kth Number in an Array&#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":[139],"tags":[],"class_list":["post-33364","post","type-post","status-publish","format-standard","hentry","category-java-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Coding Test Course, Finding the Kth Number in an Array - \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\/33364\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Coding Test Course, Finding the Kth Number in an Array - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this lecture, we will address one of the commonly tested algorithm problems in Java, which is the &#8220;Finding the Kth Smallest Number in an Array&#8221; problem. This problem involves finding the Kth smallest number in a given array and can be approached in various ways. The ability to solve such types of problems is &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, Finding the Kth Number in an Array&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33364\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:15:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:39:00+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33364\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33364\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, Finding the Kth Number in an Array\",\"datePublished\":\"2024-11-01T09:15:54+00:00\",\"dateModified\":\"2024-11-01T11:39:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33364\/\"},\"wordCount\":396,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33364\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33364\/\",\"name\":\"Java Coding Test Course, Finding the Kth Number in an Array - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:15:54+00:00\",\"dateModified\":\"2024-11-01T11:39:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33364\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33364\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33364\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, Finding the Kth Number in an Array\"}]},{\"@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":"Java Coding Test Course, Finding the Kth Number in an Array - \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\/33364\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, Finding the Kth Number in an Array - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this lecture, we will address one of the commonly tested algorithm problems in Java, which is the &#8220;Finding the Kth Smallest Number in an Array&#8221; problem. This problem involves finding the Kth smallest number in a given array and can be approached in various ways. The ability to solve such types of problems is &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, Finding the Kth Number in an Array\"","og_url":"https:\/\/atmokpo.com\/w\/33364\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:15:54+00:00","article_modified_time":"2024-11-01T11:39:00+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33364\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33364\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, Finding the Kth Number in an Array","datePublished":"2024-11-01T09:15:54+00:00","dateModified":"2024-11-01T11:39:00+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33364\/"},"wordCount":396,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33364\/","url":"https:\/\/atmokpo.com\/w\/33364\/","name":"Java Coding Test Course, Finding the Kth Number in an Array - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:15:54+00:00","dateModified":"2024-11-01T11:39:00+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33364\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33364\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33364\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, Finding the Kth Number in an Array"}]},{"@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\/33364","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=33364"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33364\/revisions"}],"predecessor-version":[{"id":33365,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33364\/revisions\/33365"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}