{"id":12759,"date":"2023-02-13T10:16:45","date_gmt":"2023-02-13T10:16:45","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=12759"},"modified":"2023-04-19T11:36:39","modified_gmt":"2023-04-19T11:36:39","slug":"kth-smallest-element-in-an-array","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/","title":{"rendered":"Kth Smallest Element in an Array"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg\" alt=\"\" \/><\/p>\n<p>An array is a data structure used in computer programming to store a collection of values, each with its own unique index or key. These values can be of any data type, such as integers, characters, or objects. Arrays allow for efficient access to and manipulation of individual elements, making them a common tool for organizing and processing data. Arrays can be <a href=\"http:\/\/https:\/\/prepbytes.com\/blog\/arrays\/one-dimensional-array\/\" title=\"one-dimensional\">one-dimensional<\/a>, where values are stored in a linear sequence, or multi-dimensional, where values are organized in a grid-like structure. The size of an array is fixed when it is created, and elements can be added or removed using built-in functions. Arrays are a fundamental concept in many programming languages and are widely used in a variety of applications. The elements can be present in any order and we will find the kth smallest element in an array.<\/p>\n<h2>Kth Smallest Element in an Array<\/h2>\n<p>Given an array of integer type containing n elements, we have to find the kth smallest element in an array. With the condition given that all the elements in the array are distinct. And the value of k will be less than equal to n where n is the number of elements in the array.<\/p>\n<h3>Examples to Understand<\/h3>\n<p>Here we will look at some of the examples corresponding to the question of finding the kth smallest element in an array.<\/p>\n<p><strong>Input<\/strong><\/p>\n<pre><code>A[]={1, 5,7,16,2,4,3,77}\n k=3<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>The 3rd smallest element of the above array is 3.<\/code><\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre><code>A[]={11, 52,7,146,24,44,3,46,75,23,42}\n k=4<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>The 4th smallest element of the above array is 23.<\/code><\/pre>\n<h2>Different Methods to Find Kth Smallest Element of an Array<\/h2>\n<p>Now we will discuss various approaches to solve the above problem i,e, find the kth smallest element in an array.<\/p>\n<h3>Approach 1: Brute Force using Sorting<\/h3>\n<p>In this approach, we will see the solution to the question of finding the kth smallest element in an array using sorting.<br \/>\nAs the name suggests it is simple to approach we will just sort the array and after that, we will find the kth element from the start of the array i.e, from the left.<\/p>\n<p><strong>Algorithm<\/strong><br \/>\nHere we will discuss the steps one needs to follow to implement the above approach.<\/p>\n<ul>\n<li><a href=\"http:\/\/https:\/\/prepbytes.com\/blog\/c-programming\/c-program-to-sort-an-array-in-ascending-order\/\" title=\"Sort the array\">Sort the array<\/a> in ascending order using any sorting algorithm or any built-in sort function if your programming language supports it.<\/li>\n<li>Then directly return the kth element from the starting of the array as you have sorted the array in ascending order.<\/li>\n<\/ul>\n<p><strong>Code Implementation<\/strong><br \/>\nIn this section, we will implement the code in various languages.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12469 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12469 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12469 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12469 .wpsm_nav-tabs > li.active > a, #tab_container_12469 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12469 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12469 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12469 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12469 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12469 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12469 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12469 .wpsm_nav-tabs > li > a:hover , #tab_container_12469 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12469 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12469 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12469 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12469 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12469 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12469 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12469 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12469 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12469 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12469 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12469 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12469\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12469\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12469_1\" aria-controls=\"tabs_desc_12469_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12469_2\" aria-controls=\"tabs_desc_12469_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12469_3\" aria-controls=\"tabs_desc_12469_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12469_4\" aria-controls=\"tabs_desc_12469_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12469\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12469_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint cmpfunc(const void* a, const void* b)\r\n{\r\n    return (*(int*)a - *(int*)b);\r\n}\r\n\r\nint kthSmallestelem(int arr[], int N, int K)\r\n{\r\n    qsort(arr, N, sizeof(int), cmpfunc);\r\n\r\n    return arr[K - 1];\r\n}\r\n\r\nint main()\r\n{\r\n    int arr[] = { 121, 13, 45, 17, 19 };\r\n    int N = sizeof(arr) \/ sizeof(arr[0]), K = 2;\r\n\r\n    printf(\"K'th smallest element is %d\",\r\n        kthSmallestelem(arr, N, K));\r\n    return 0;\r\n}\r\n\r\n\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12469_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\nint kthSmallestelem(int arr[], int N, int K)\r\n{\r\n    sort(arr, arr + N);\r\n\r\n    return arr[K - 1];\r\n}\r\n\r\nint main()\r\n{\r\n    int arr[] = { 121, 13, 45, 17, 19 };\r\n    int N = sizeof(arr) \/ sizeof(arr[0]), K = 2;\r\n\r\n    cout &lt;&lt; \"K'th smallest element is \"\r\n        &lt;&lt; kthSmallestelem(arr, N, K);\r\n    return 0;\r\n}\r\n\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12469_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.util.Arrays;\r\nimport java.util.Collections;\r\n\r\nclass PrepBytes {\r\n    public static int kthSmallestelem(Integer[] arr, int K)\r\n    {\r\n        Arrays.sort(arr);\r\n\r\n        return arr[K - 1];\r\n    }\r\n\r\n    public static void main(String[] args)\r\n    {\r\n        Integer arr[] = new Integer[] { 121, 13, 45, 17, 19 };\r\n        int K = 2;\r\n\r\n        System.out.print(\"K'th smallest element is \"\r\n                        + kthSmallestelem(arr, K));\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12469_4\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def kthSmallestelem(arr, N, K):\r\n\r\n    arr.sort()\r\n    return arr[K-1]\r\n\r\n\r\nif __name__ == '__main__':\r\n    arr = [ 121, 13, 45, 17, 19 ]\r\n    N = len(arr)\r\n    K = 2\r\n\r\n    print(\"K'th smallest element is\",\r\n        kthSmallestelem(arr, N, K))\r\n\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12469 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12469 a\"),jQuery(\"#tab-content_12469\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>K'th smallest element is 17<\/code><\/pre>\n<p><strong>Explanation of the above code<\/strong><br \/>\nWe have followed a similar approach in all of the codes given above no matter of the language we have passed the array, its size, and the k to the fucntionKthSmallestelem which will first sort the array and then return the corresponding answer i.e, kth smallest element in an array.<\/p>\n<p><strong>Time Complexity<\/strong><br \/>\nThe time complexity of this approach will be O(nlogn) as we are sorting the array and the sorting of the array will take O(nlogn) for best algorithms.<\/p>\n<p><strong>Space Complexity<\/strong><br \/>\nThe auxiliary space complexity for this approach will be O(1) as we are not using any additional space.<\/p>\n<h3>Approach 2: Using Min Heap<\/h3>\n<p>Here we will see the solution approach using min heap and find the kth smallest element in an array. So the approach is very simple in this we just have to put all the elements in the min heap and after that, we have to get the min element from the heap k times.<\/p>\n<p><strong>Algorithm<\/strong><br \/>\nThe algorithm contains three steps.<\/p>\n<ul>\n<li>Insert all the elements of the array in the min heap.<\/li>\n<li>Now call the extractMin() function k times.<\/li>\n<li>Now return the last value given by the extractMin() function.<\/li>\n<\/ul>\n<p><strong>Code Implementation<\/strong><br \/>\nIn this section, we will implement the code in various languages.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12470 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12470 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12470 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12470 .wpsm_nav-tabs > li.active > a, #tab_container_12470 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12470 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12470 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12470 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12470 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12470 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12470 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12470 .wpsm_nav-tabs > li > a:hover , #tab_container_12470 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12470 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12470 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12470 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12470 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12470 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12470 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12470 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12470 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12470 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12470 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12470 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12470\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12470\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12470_1\" aria-controls=\"tabs_desc_12470_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12470_2\" aria-controls=\"tabs_desc_12470_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12470_3\" aria-controls=\"tabs_desc_12470_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12470\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12470_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;climits&gt;\r\n#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nvoid swap(int* x, int* y);\r\n\r\nclass MinHeap {\r\n\r\n    int* harr; \r\n    int capacity; \r\n    int heap_size; \r\npublic:\r\n    MinHeap(int a[], int size); \r\n\r\n    \r\n    void MinHeapify(int i);\r\n    int parent(int i) { return (i - 1) \/ 2; }\r\n    int left(int i) { return (2 * i + 1); }\r\n    int right(int i) { return (2 * i + 2); }\r\n\r\n    int extractMin(); \r\n    int getMin() { return harr[0]; } \r\n};\r\n\r\nMinHeap::MinHeap(int a[], int size)\r\n{\r\n    heap_size = size;\r\n    harr = a; \r\n    int i = (heap_size - 1) \/ 2;\r\n    while (i &gt;= 0) {\r\n        MinHeapify(i);\r\n        i--;\r\n    }\r\n}\r\nint MinHeap::extractMin()\r\n{\r\n    if (heap_size == 0)\r\n        return INT_MAX;\r\n\r\n    \r\n    int root = harr[0];\r\n\r\n\r\n    if (heap_size &gt; 1) {\r\n        harr[0] = harr[heap_size - 1];\r\n        MinHeapify(0);\r\n    }\r\n    heap_size--;\r\n\r\n    return root;\r\n}\r\nvoid MinHeap::MinHeapify(int i)\r\n{\r\n    int l = left(i);\r\n    int r = right(i);\r\n    int smallest = i;\r\n\r\n    if (l &lt; heap_size &amp;&amp; harr[l] &lt; harr[i])\r\n        smallest = l;\r\n\r\n    if (r &lt; heap_size &amp;&amp; harr[r] &lt; harr[smallest])\r\n        smallest = r;\r\n\r\n    if (smallest != i) {\r\n        swap(&amp;harr[i], &amp;harr[smallest]);\r\n        MinHeapify(smallest);\r\n    }\r\n}\r\n\r\nvoid swap(int* x, int* y)\r\n{\r\n    int temp = *x;\r\n    *x = *y;\r\n    *y = temp;\r\n}\r\nint kthSmallest(int arr[], int N, int K)\r\n{\r\n    MinHeap mh(arr, N);\r\n\r\n    for (int i = 0; i &lt; K - 1; i++)\r\n        mh.extractMin();\r\n\r\n    return mh.getMin();\r\n}\r\n\r\nint main()\r\n{\r\n    int arr[] = { 42, 49, 12, 11, 73, 85, 39 };\r\n    int N = sizeof(arr) \/ sizeof(arr[0]), K = 4;\r\n\r\n    cout &lt;&lt; \"K'th smallest element is \"\r\n        &lt;&lt; kthSmallest(arr, N, K);\r\n    return 0;\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12470_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.util.*;\r\n class PrepBytes {\r\n\r\n    class MinHeap {\r\n        int[] harr; \r\n        int capacity;\r\n        int heap_size; \r\n\r\n        int parent(int i) { return (i - 1) \/ 2; }\r\n        int left(int i) { return ((2 * i) + 1); }\r\n        int right(int i) { return ((2 * i) + 2); }\r\n        int getMin() { return harr[0]; } \/\/ Returns minimum\r\n\r\n        void replaceMax(int x)\r\n        {\r\n            this.harr[0] = x;\r\n            minHeapify(0);\r\n        }\r\n        MinHeap(int a[], int size)\r\n        {\r\n            heap_size = size;\r\n            harr = a; \r\n            int i = (heap_size - 1) \/ 2;\r\n            while (i &gt;= 0) {\r\n                minHeapify(i);\r\n                i--;\r\n            }\r\n        }\r\n\r\n        int extractMin()\r\n        {\r\n            if (heap_size == 0)\r\n                return Integer.MAX_VALUE;\r\n\r\n            int root = harr[0];\r\n\r\n            \r\n            if (heap_size &gt; 1) {\r\n                harr[0] = harr[heap_size - 1];\r\n                minHeapify(0);\r\n            }\r\n            heap_size--;\r\n            return root;\r\n        }\r\n\r\n\r\n        void minHeapify(int i)\r\n        {\r\n            int l = left(i);\r\n            int r = right(i);\r\n            int smallest = i;\r\n            if (l &lt; heap_size &amp;&amp; harr[l] &lt; harr[i])\r\n                smallest = l;\r\n            if (r &lt; heap_size &amp;&amp; harr[r] &lt; harr[smallest])\r\n                smallest = r;\r\n            if (smallest != i) {\r\n                int t = harr[i];\r\n                harr[i] = harr[smallest];\r\n                harr[smallest] = t;\r\n                minHeapify(smallest);\r\n            }\r\n        }\r\n    };\r\n\r\n    int kthSmallest(int arr[], int N, int K)\r\n    {\r\n\r\n        MinHeap mh = new MinHeap(arr, N);\r\n\r\n\r\n        for (int i = 0; i &lt; K - 1; i++)\r\n            mh.extractMin();\r\n\r\n        return mh.getMin();\r\n    }\r\n\r\n    public static void main(String[] args)\r\n    {\r\n        int arr[] = { 42, 49, 12, 11, 73, 85, 39 };\r\n        int N = arr.length, K = 2;\r\n        PrepBytes Prep = new PrepBytes();\r\n\r\n        System.out.print(\"K'th smallest element is \"\r\n                        + Prep.kthSmallest(arr, N, K));\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12470_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">class MinHeap:\r\n\r\n    def __init__(self, a, size):\r\n\r\n        self.harr = a\r\n\r\n        self.capacity = None\r\n\r\n        self.heap_size = size\r\n\r\n        i = int((self.heap_size - 1) \/ 2)\r\n        while i &gt;= 0:\r\n            self.minHeapify(i)\r\n            i -= 1\r\n\r\n    def parent(self, i):\r\n        return (i - 1) \/ 2\r\n\r\n    def left(self, i):\r\n        return 2 * i + 1\r\n\r\n    def right(self, i):\r\n        return 2 * i + 2\r\n\r\n    def getMin(self):\r\n        return self.harr[0]\r\n\r\n    def extractMin(self):\r\n        if self.heap_size == 0:\r\n            return float(\"inf\")\r\n\r\n        root = self.harr[0]\r\n\r\n        if self.heap_size &gt; 1:\r\n            self.harr[0] = self.harr[self.heap_size - 1]\r\n            self.minHeapify(0)\r\n        self.heap_size -= 1\r\n        return root\r\n\r\n    def minHeapify(self, i):\r\n        l = self.left(i)\r\n        r = self.right(i)\r\n        smallest = i\r\n        if ((l &lt; self.heap_size) and\r\n                (self.harr[l] &lt; self.harr[i])):\r\n            smallest = l\r\n        if ((r &lt; self.heap_size) and\r\n                (self.harr[r] &lt; self.harr[smallest])):\r\n            smallest = r\r\n        if smallest != i:\r\n            self.harr[i], self.harr[smallest] = (\r\n                self.harr[smallest], self.harr[i])\r\n            self.minHeapify(smallest)\r\n\r\ndef kthSmallest(arr, N, K):\r\n\r\n    mh = MinHeap(arr, N)\r\n\r\n    for i in range(K - 1):\r\n        mh.extractMin()\r\n\r\n    return mh.getMin()\r\n\r\n\r\nif __name__ == '__main__':\r\n    arr = [ 42, 49, 12, 11, 73, 85, 39 ]\r\n    N = len(arr)\r\n    K = 4\r\n\r\n    print(\"K'th smallest element is\", kthSmallest(arr, N, K))\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12470 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12470 a\"),jQuery(\"#tab-content_12470\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>K'th smallest element is 42<\/code><\/pre>\n<p><strong>Explanation of the above code<\/strong><br \/>\nIn all the code above we have followed the approach of min heap where we have put all the elements in min heap and after getting the minimum element k times we get our kth smallest element in an array.<\/p>\n<p><strong>Time Complexity<\/strong><br \/>\nThe time complexity of the above approach will be O(N+KlogN) where N is the size of the array and KlogN will be for the min heap as the elements are stored in sorted order.<\/p>\n<p><strong>Space Complexity<\/strong><br \/>\nThe space complexity of the above approach will be O(N) as we are storing all the elements of the array in the heap.<\/p>\n<h3>Approach 3: Using MaxHeap<\/h3>\n<p>By using the approach of max heap we can find the kth smallest element as here we have to put the first k elements in the max heap after that we have to compare the remaining elements with the root of the max heap if the root value is greater than the current element value then we will remove the root from the max heap and insert the current element.<\/p>\n<p><strong>Algorithm<\/strong><br \/>\nThe algorithm consists of some steps that we need to follow while working with the max heap approach to finding the kth smallest element in an array.<\/p>\n<ul>\n<li>Build a max heap and insert the first k elements of the array in that max heap.<\/li>\n<li>For the remaining element compare each of them with the root of the max heap.<\/li>\n<li>If the value of the root of max heap is greater than the current element then remove the root of maxheap.<\/li>\n<li>Now after all the complete iterations of the array, we will have the root element of the max heap as the kth smallest element.<\/li>\n<\/ul>\n<p><strong>Code Implementation<\/strong><br \/>\nIn this section, we will see the code implementation of the above approach in various programming languages.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12471 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12471 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12471 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12471 .wpsm_nav-tabs > li.active > a, #tab_container_12471 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12471 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12471 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12471 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12471 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12471 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12471 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12471 .wpsm_nav-tabs > li > a:hover , #tab_container_12471 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12471 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12471 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12471 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12471 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12471 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12471 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12471 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12471 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12471 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12471 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12471 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12471\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12471\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12471_1\" aria-controls=\"tabs_desc_12471_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12471_2\" aria-controls=\"tabs_desc_12471_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12471_3\" aria-controls=\"tabs_desc_12471_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12471\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12471_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">\/\/ C++ program to find k'th smallest element using max heap\r\n\r\n#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\nvoid swap(int* x, int* y);\r\n\r\nclass MaxHeap {\r\n\r\n    int* harr; \r\n    int capacity; \r\n    int heap_size; \r\n\r\npublic:\r\n    MaxHeap(int a[], int size); \r\n    void maxHeapify(\r\n        int i); \r\n    int parent(int i) { return (i - 1) \/ 2; }\r\n    int left(int i) { return (2 * i + 1); }\r\n    int right(int i) { return (2 * i + 2); }\r\n\r\n    int extractMax();\r\n    int getMax() { return harr[0]; }\r\n\r\n    void replaceMax(int x)\r\n    {\r\n        harr[0] = x;\r\n        maxHeapify(0);\r\n    }\r\n};\r\n\r\nMaxHeap::MaxHeap(int a[], int size)\r\n{\r\n    heap_size = size;\r\n    harr = a; \/\/ store address of array\r\n    int i = (heap_size - 1) \/ 2;\r\n    while (i &gt;= 0) {\r\n        maxHeapify(i);\r\n        i--;\r\n    }\r\n}\r\n\r\nint MaxHeap::extractMax()\r\n{\r\n    if (heap_size == 0)\r\n        return INT_MAX;\r\n\r\n    int root = harr[0];\r\n\r\n    if (heap_size &gt; 1) {\r\n        harr[0] = harr[heap_size - 1];\r\n        maxHeapify(0);\r\n    }\r\n    heap_size--;\r\n\r\n    return root;\r\n}\r\n\r\n\r\nvoid MaxHeap::maxHeapify(int i)\r\n{\r\n    int l = left(i);\r\n    int r = right(i);\r\n    int largest = i;\r\n\r\n    if (l &lt; heap_size &amp;&amp; harr[l] &gt; harr[i])\r\n        largest = l;\r\n\r\n    if (r &lt; heap_size &amp;&amp; harr[r] &gt; harr[largest])\r\n        largest = r;\r\n\r\n    if (largest != i) {\r\n        swap(&amp;harr[i], &amp;harr[largest]);\r\n        maxHeapify(largest);\r\n    }\r\n}\r\n\r\nvoid swap(int* x, int* y)\r\n{\r\n    int temp = *x;\r\n    *x = *y;\r\n    *y = temp;\r\n}\r\n\r\nint kthSmallestelem(int arr[], int N, int K)\r\n{\r\n    MaxHeap mh(arr, K);\r\n\r\n    \r\n    for (int i = K; i &lt; N; i++)\r\n        if (arr[i] &lt; mh.getMax())\r\n            mh.replaceMax(arr[i]);\r\n\r\n    return mh.getMax();\r\n}\r\n\r\nint main()\r\n{\r\n    int arr[] = { 42, 49, 12, 11, 73, 85, 39 };\r\n    int N = sizeof(arr) \/ sizeof(arr[0]), K = 3;\r\n\r\n    cout &lt;&lt; \"K'th smallest element is \"\r\n        &lt;&lt; kthSmallestelem(arr, N, K);\r\n    return 0;\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12471_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.util.*;\r\nclass PrepBytes {\r\n\r\n\r\n    class MaxHeap {\r\n        int[] harr; \r\n        int capacity; \r\n        int heap_size; \r\n                    \r\n\r\n        int parent(int i) { return (i - 1) \/ 2; }\r\n        int left(int i) { return (2 * i + 1); }\r\n        int right(int i) { return (2 * i + 2); }\r\n        int getMax() { return harr[0]; } \r\n\r\n        void replaceMax(int x)\r\n        {\r\n            this.harr[0] = x;\r\n            maxHeapify(0);\r\n        }\r\n        MaxHeap(int a[], int size)\r\n        {\r\n            heap_size = size;\r\n            harr = a;\r\n            int i = (heap_size - 1) \/ 2;\r\n            while (i &gt;= 0) {\r\n                maxHeapify(i);\r\n                i--;\r\n            }\r\n        }\r\n\r\n    \r\n        int extractMax()\r\n        {\r\n            if (heap_size == 0)\r\n                return Integer.MAX_VALUE;\r\n\r\n            int root = harr[0];\r\n\r\n    \r\n            if (heap_size &gt; 1) {\r\n                harr[0] = harr[heap_size - 1];\r\n                maxHeapify(0);\r\n            }\r\n            heap_size--;\r\n            return root;\r\n        }\r\n\r\n        void maxHeapify(int i)\r\n        {\r\n            int l = left(i);\r\n            int r = right(i);\r\n            int largest = i;\r\n            if (l &lt; heap_size &amp;&amp; harr[l] &gt; harr[i])\r\n                largest = l;\r\n            if (r &lt; heap_size &amp;&amp; harr[r] &gt; harr[largest])\r\n                largest = r;\r\n            if (largest != i) {\r\n                int t = harr[i];\r\n                harr[i] = harr[largest];\r\n                harr[largest] = t;\r\n                maxHeapify(largest);\r\n            }\r\n        }\r\n    };\r\n\r\n    \r\n    int kthSmallestelem(int arr[], int N, int K)\r\n    {\r\n\r\n        MaxHeap mh = new MaxHeap(arr, K);\r\n\r\n        for (int i = K; i &lt; N; i++)\r\n            if (arr[i] &lt; mh.getMax())\r\n                mh.replaceMax(arr[i]);\r\n\r\n        return mh.getMax();\r\n    }\r\n\r\n    public static void main(String[] args)\r\n    {\r\n        int arr[] = { 42, 49, 12, 11, 73, 85, 39 };\r\n        int N = arr.length, K = 3;\r\n        PrepBytes Prep = new PrepBytes();\r\n\r\n        System.out.print(\"K'th smallest element is \"\r\n                        + Prep.kthSmallestelem(arr, N, K));\r\n    }\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12471_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">class MaxHeap:\r\n    def __init__(self, a, size):\r\n        self.harr = a\r\n        self.capacity = None\r\n        self.heap_size = size\r\n\r\n        i = int((self.heap_size - 1) \/ 2)\r\n        while i &gt;= 0:\r\n            self.maxHeapify(i)\r\n            i -= 1\r\n\r\n    def parent(self, i):\r\n        return (i - 1) \/ 2\r\n\r\n    def left(self, i):\r\n        return 2 * i + 1\r\n\r\n    def right(self, i):\r\n        return 2 * i + 2\r\n\r\n    def getMax(self):\r\n        return self.harr[0]\r\n\r\n    def replaceMax(self, x):\r\n        self.harr[0] = x\r\n        self.maxHeapify(0)\r\n\r\n    \r\n    def extractMin(self):\r\n        if self.heap_size == 0:\r\n            return float(\"inf\")\r\n\r\n        # Store the maximum value.\r\n        root = self.harr[0]\r\n\r\n    \r\n        if self.heap_size &gt; 1:\r\n            self.harr[0] = self.harr[self.heap_size - 1]\r\n            self.maxHeapify(0)\r\n        self.heap_size -= 1\r\n        return root\r\n\r\n    def maxHeapify(self, i):\r\n        l = self.left(i)\r\n        r = self.right(i)\r\n        largest = i\r\n\r\n        if ((l &lt; self.heap_size) and\r\n                (self.harr[l] &gt; self.harr[i])):\r\n            largest = l\r\n\r\n        if ((r &lt; self.heap_size) and\r\n                (self.harr[r] &gt; self.harr[largest])):\r\n            largest = r\r\n\r\n        if largest != i:\r\n            self.harr[i], self.harr[largest] = (\r\n                self.harr[largest], self.harr[i])\r\n            self.maxHeapify(largest)\r\n\r\ndef kthSmallestelem(arr, N, K):\r\n    mh = MaxHeap(arr, K)\r\n\r\n    for i in range(K, N):\r\n        if arr[i] &lt; mh.getMax():\r\n            mh.replaceMax(arr[i])\r\n\r\n    return mh.getMax()\r\n\r\n\r\nif __name__ == '__main__':\r\n    arr = [42, 49, 12, 11, 73, 85, 39]\r\n    N = len(arr)\r\n    K = 3\r\n\r\n    print(\"K'th smallest element is\", kthSmallestelem(arr, N, K))\r\n\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12471 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12471 a\"),jQuery(\"#tab-content_12471\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>K'th smallest element is 39<\/code><\/pre>\n<p><strong>Explanation of the above code<\/strong><br \/>\nIn the above code we are using the max heap and implementing the max heap from the scratch after that we are calling the kthSmallestelem function which will give the result by following the approach and algorithm explained above.<\/p>\n<p><strong>Time Complexity<\/strong><br \/>\nThe time complexity of the above approach will be O(K+ (N-K)*LogK) because first we will insert the k elements in the max heap after that in the worst case we have to change the root element for the rest N-K times.<\/p>\n<p><strong>Space Complexity<\/strong><br \/>\nThe space complexity for the above approach will be O(K) because we are having only K elements in the max heap all the time. <\/p>\n<h3>Approach 4: Using Quick Select<\/h3>\n<p>This approach is similar to the first approach where we have used sorting, So suppose in the first approach we have used quick sort but instead of sorting the full array we have stopped in between then this will be quick select. In this approach, we will pick an element and move the element to its correct position and that element will partition the array and will check if that is not the kth element if yes then we will stop, and if not then we will not recur on both left and right parts we will recur on the corresponding part according to the position of the pivot.<\/p>\n<p><strong>Algorithm<\/strong><br \/>\nHere we will discuss the algorithm of the above approach.<\/p>\n<ul>\n<li>Apply the quick sort algorithm on the array.<\/li>\n<li>Now move the pivot element to its correct position.<\/li>\n<li>After that, we will check the index of the pivot element if the index is equal to k then we will stop.<\/li>\n<li>Otherwise, if the index of the pivot element is less than k then we will recur for the right subarray else we will recur for the left subarray.<\/li>\n<li>Will repeat this process until we have not found the kth smallest element in an array.<\/li>\n<\/ul>\n<p><strong>Code Implementation<\/strong><br \/>\nNow we will see the code implementation in various languages.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12472 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12472 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12472 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12472 .wpsm_nav-tabs > li.active > a, #tab_container_12472 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12472 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12472 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12472 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12472 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12472 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12472 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12472 .wpsm_nav-tabs > li > a:hover , #tab_container_12472 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12472 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12472 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12472 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12472 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12472 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12472 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12472 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12472 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12472 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12472 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12472 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12472\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12472\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12472_1\" aria-controls=\"tabs_desc_12472_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12472_2\" aria-controls=\"tabs_desc_12472_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12472_3\" aria-controls=\"tabs_desc_12472_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12472_4\" aria-controls=\"tabs_desc_12472_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12472\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12472_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;limits.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\nint partition(int arr[], int l, int r);\r\n\r\nint kthSmallestelem(int arr[], int l, int r, int K)\r\n{\r\n    if (K &gt; 0 &amp;&amp; K &lt;= r - l + 1) {\r\n\r\n        int pos = partition(arr, l, r);\r\n\r\n        if (pos - l == K - 1)\r\n            return arr[pos];\r\n        if (pos - l &gt; K - 1) \r\n                        \r\n            return kthSmallestelem(arr, l, pos - 1, K);\r\n\r\n        return kthSmallestelem(arr, pos + 1, r,\r\n                        K - pos + l - 1);\r\n    }\r\n\r\n    return INT_MAX;\r\n}\r\n\r\nvoid swap(int* a, int* b)\r\n{\r\n    int temp = *a;\r\n    *a = *b;\r\n    *b = temp;\r\n}\r\nint partition(int arr[], int l, int r)\r\n{\r\n    int x = arr[r], i = l;\r\n    for (int j = l; j &lt;= r - 1; j++) {\r\n        if (arr[j] &lt;= x) {\r\n            swap(&amp;arr[i], &amp;arr[j]);\r\n            i++;\r\n        }\r\n    }\r\n\r\n    swap(&amp;arr[i], &amp;arr[r]);\r\n    return i;\r\n}\r\n\r\nint main()\r\n{\r\n    int arr[] = { 42, 49, 12, 11, 73, 85, 39 };\r\n    int N = sizeof(arr) \/ sizeof(arr[0]), K = 4;\r\n    printf(\"K'th smallest element is %d\",\r\n        kthSmallestelem(arr, 0, N - 1, K));\r\n    return 0;\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12472_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\nint partition(int arr[], int l, int r);\r\nint kthSmallestelem(int arr[], int l, int r, int K)\r\n{\r\n    if (K &gt; 0 &amp;&amp; K &lt;= r - l + 1) {\r\n\r\n        int pos = partition(arr, l, r);\r\n\r\n        if (pos - l == K - 1)\r\n            return arr[pos];\r\n        if (pos - l &gt; K - 1) \r\n            return kthSmallestelem(arr, l, pos - 1, K);\r\n\r\n        return kthSmallestelem(arr, pos + 1, r,\r\n                        K - pos + l - 1);\r\n    }\r\n\r\n    return INT_MAX;\r\n}\r\n\r\nvoid swap(int* a, int* b)\r\n{\r\n    int temp = *a;\r\n    *a = *b;\r\n    *b = temp;\r\n}\r\n\r\nint partition(int arr[], int l, int r)\r\n{\r\n    int x = arr[r], i = l;\r\n    for (int j = l; j &lt;= r - 1; j++) {\r\n        if (arr[j] &lt;= x) {\r\n            swap(&amp;arr[i], &amp;arr[j]);\r\n            i++;\r\n        }\r\n    }\r\n\r\n    swap(&amp;arr[i], &amp;arr[r]);\r\n    return i;\r\n}\r\nint main()\r\n{\r\n    int arr[] = { 42, 49, 12, 11, 73, 85, 39 };\r\n    int N = sizeof(arr) \/ sizeof(arr[0]), K = 4;\r\n    cout &lt;&lt; \"K'th smallest element is \"\r\n        &lt;&lt; kthSmallestelem(arr, 0, N - 1, K);\r\n    return 0;\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12472_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.util.Arrays;\r\nimport java.util.Collections;\r\n\r\nclass PrepBytes {\r\n\r\n    public static int partition(Integer[] arr, int l, int r)\r\n    {\r\n        int x = arr[r], i = l;\r\n        for (int j = l; j &lt;= r - 1; j++) {\r\n            if (arr[j] &lt;= x) {\r\n                int temp = arr[i];\r\n                arr[i] = arr[j];\r\n                arr[j] = temp;\r\n\r\n                i++;\r\n            }\r\n        }\r\n\r\n        int temp = arr[i];\r\n        arr[i] = arr[r];\r\n        arr[r] = temp;\r\n\r\n        return i;\r\n    }\r\n\r\n    public static int kthSmallestelem(Integer[] arr, int l,\r\n                                int r, int K)\r\n    {\r\n        if (K &gt; 0 &amp;&amp; K &lt;= r - l + 1) {\r\n\r\n            int pos = partition(arr, l, r);\r\n\r\n            if (pos - l == K - 1)\r\n                return arr[pos];\r\n\r\n            if (pos - l &gt; K - 1)\r\n                return kthSmallestelem(arr, l, pos - 1, K);\r\n\r\n            return kthSmallestelem(arr, pos + 1, r,\r\n                            K - pos + l - 1);\r\n        }\r\n\r\n        return Integer.MAX_VALUE;\r\n    }\r\n\r\n    public static void main(String[] args)\r\n    {\r\n        Integer arr[]\r\n            = new Integer[] { 42, 49, 12, 11, 73, 85, 39 };;\r\n        int K = 4;\r\n\r\n        System.out.print(\r\n            \"K'th smallest element is \"\r\n            + kthSmallestelem(arr, 0, arr.length - 1, K));\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12472_4\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import sys\r\n\r\n\r\ndef kthSmallestelem(arr, l, r, K):\r\n\r\n    \r\n    if (K &gt; 0 and K &lt;= r - l + 1):\r\n\r\n    \r\n        pos = partition(arr, l, r)\r\n\r\n        if (pos - l == K - 1):\r\n            return arr[pos]\r\n        if (pos - l &gt; K - 1): \r\n            return kthSmallestelem(arr, l, pos - 1, K)\r\n\r\n        return kthSmallestelem(arr, pos + 1, r,\r\n                        K - pos + l - 1)\r\n\r\n\r\n    return sys.maxsize\r\n\r\n\r\ndef partition(arr, l, r):\r\n\r\n    x = arr[r]\r\n    i = l\r\n    for j in range(l, r):\r\n        if (arr[j] &lt;= x):\r\n            arr[i], arr[j] = arr[j], arr[i]\r\n            i += 1\r\n    arr[i], arr[r] = arr[r], arr[i]\r\n    return i\r\nif __name__ == \"__main__\":\r\n\r\n    arr = [42, 49, 12, 11, 73, 85, 39]\r\n    N = len(arr)\r\n    K = 4\r\n    print(\"K'th smallest element is\",\r\n        kthSmallestelem(arr, 0, N - 1, K))\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12472 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12472 a\"),jQuery(\"#tab-content_12472\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>K'th smallest element is 42<\/code><\/pre>\n<p><strong>Explanation of the above code<\/strong><br \/>\nIn the above code, we have explained the quick select approach where we are using the quicksort until we find the kth smallest element in an array but with slight modifications as we are recurring on the left and right subarray according to the position of the pivot element.<\/p>\n<p><strong>Time Complexity<\/strong><br \/>\nThe time complexity of the above method will be O(N^2) in the worst case and O(N) in the average case as in the worst case we have to perform the complete quicksort.<\/p>\n<p><strong>Space Complexity<\/strong><br \/>\nThe space complexity of the above approach will be O(1) as we are not usng any additional space.<\/p>\n<h3>Approach 5: Using Map and Frequency of Elements<\/h3>\n<p>This approach is a bit similar to counting sort and Quickselect but it is much easier to implement as we are using the ordered map in this case which will store the elements in ascending order so we will store all the elements of the array in the map and after that, we will travel in the map by adding the frequency of the elements and will move till the sum of frequency is less than k.<\/p>\n<p><strong>Algorithm<\/strong><br \/>\nNow, we will look at the algorithm of the above approach.<\/p>\n<ul>\n<li>Store the frequency of each element of the array in the map<\/li>\n<li>Now travel in the map and start to sum the frequencies of the element encountered till now.<\/li>\n<li>The point or element at which the sum of frequency is greater than equal to k.<\/li>\n<li>Then return the corresponding element.<\/li>\n<\/ul>\n<p><strong>Code Implementation<\/strong><br \/>\nNow, we will look at the code implementation of the above approach in various languages.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12473 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12473 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12473 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12473 .wpsm_nav-tabs > li.active > a, #tab_container_12473 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12473 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12473 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12473 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12473 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12473 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12473 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12473 .wpsm_nav-tabs > li > a:hover , #tab_container_12473 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12473 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12473 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12473 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12473 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12473 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12473 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12473 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12473 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12473 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12473 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12473 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12473\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12473\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12473_1\" aria-controls=\"tabs_desc_12473_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12473_2\" aria-controls=\"tabs_desc_12473_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12473_3\" aria-controls=\"tabs_desc_12473_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12473\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12473_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\nint Kth_smallestelem(map&lt;int, int&gt; mp, int K)\r\n{\r\n    int freq = 0;\r\n    for (auto it = mp.begin(); it != mp.end(); it++) {\r\n        freq += (it-&gt;second); \r\n\r\n        if (freq &gt;= K)  \r\n        {\r\n            return it-&gt;first;\r\n        }\r\n    }\r\n\r\n    return -1;  \r\n}\r\n\r\nint main()\r\n{\r\n    int N = 7;\r\n    int K = 2;\r\n    vector&lt;int&gt; arr = { 42, 49, 12, 11, 73, 85, 39 };\r\n\r\n    map&lt;int, int&gt; mp;\r\n    for (int i = 0; i &lt; N; i++) {\r\n        mp[arr[i]] += 1; \r\n    }\r\n\r\n    int ans = Kth_smallestelem(mp, K);\r\n    cout &lt;&lt; \"The Kth smallest element is \" &lt;&lt; ans\r\n        &lt;&lt; endl;\r\n\r\n    return 0;\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12473_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\/\/ Java program for the above approach\r\n\r\nimport java.util.*;\r\n\r\nclass PrepBytes {\r\n    static int Kth_smallestelem(TreeMap&lt;Integer, Integer&gt; mp,\r\n                            int K)\r\n    {\r\n        int freq = 0;\r\n        for (Map.Entry it : mp.entrySet()) {\r\n\r\n            freq += (int)it.getValue();\r\n\r\n            \r\n            if (freq &gt;= K) {\r\n                return (int)it.getKey();\r\n            }\r\n        }\r\n\r\n        return -1; \r\n    }\r\n\r\n    public static void main(String[] args)\r\n    {\r\n        int N = 7;\r\n        int K = 2;\r\n        int[] arr = { 42, 49, 12, 11, 73, 85, 39 };\r\n        TreeMap&lt;Integer, Integer&gt; mp = new TreeMap&lt;&gt;();\r\n\r\n        for (int i = 0; i &lt; N; i++) {\r\n\r\n            mp.put(arr[i], mp.getOrDefault(arr[i], 0) + 1);\r\n        }\r\n\r\n        int ans = Kth_smallestelem(mp, K);\r\n\r\n        System.out.println(\r\n            \"The Kth smallest element is \" + ans);\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12473_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def Kth_smallestelem(mp, K):\r\n\r\n    freq = 0\r\n    for it in sorted(mp.keys()):\r\n        freq += mp[it] \r\n        if freq &gt;= K: \r\n            return it \r\n            \r\n    return -1\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    N = 7\r\n    K = 2\r\n    arr = [42, 49, 12, 11, 73, 85, 39 ]\r\n    mp = {}\r\n    for i in range(N):\r\n        if arr[i] in mp: \r\n            mp[arr[i]] = mp[arr[i]] + 1 # frequency\r\n        else:\r\n            mp[arr[i]] = 1\r\n\r\n    # Function call\r\n    ans = Kth_smallestelem(mp, K)\r\n    print(\"The \", K, \"th smallest element is \", ans)\r\n\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12473 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12473 a\"),jQuery(\"#tab-content_12473\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>The  Kth smallest element is  12<\/code><\/pre>\n<p><strong>Explanation of the above code<\/strong><br \/>\nIn the above code, we have used the map, and after that, we have used the above approach to find the kth smallest element in an array using the summation of frequency.<\/p>\n<p><strong>Time Complexity<\/strong><br \/>\nThe time complexity of the above approach will be O(NlogN) as we are storing the elements in an ordered map.<\/p>\n<p><strong>Space Complexity<\/strong><br \/>\nThe space complexity of the above method is O(N) as we are storing all the elements in the map.<\/p>\n<h3>Approach 6: Using Priority Queue<\/h3>\n<p>Here we will insert the elements into the priority queue until the size of it is less than equal to k after that we will compare all the remaining elements with the top element of the priority queue if the remaining element is less than the top of priority queue then we will remove the top and insert the current element in the priority queue and after the traversal of the array the top element of the priority queue will be the kth smallest element in an array.<\/p>\n<p><strong>Algorithm<\/strong><br \/>\nThe algorithm for the above approach is straightforward as<\/p>\n<ul>\n<li>We have to insert the first k elements in the priority queue.<\/li>\n<li>After that we will check the top element of the priority queue and compare it with the remaining elements<\/li>\n<li>If the current remaining element is less than the top element of the priority queue then we will remove the top element from the priority queue and insert the current element.<\/li>\n<li>We will continue this process until we have not traversed the whole array.<\/li>\n<li>Now the top element of the priority queue will be our kth smallest element in an array.<\/li>\n<\/ul>\n<p><strong>Dry Run for the above Approach<\/strong><br \/>\nHere we will perform the dry run of the above algorithm by taking an example.<\/p>\n<ol>\n<li>We have a given array.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203613-Kth%20Smallest%20Element%20in%20an%20Array1.png\" alt=\"\" \/><\/p>\n<ol start=\"2\">\n<li>Now we will insert the first k elements in the priority queue after that the priority queue will look like this:<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203613-Kth%20Smallest%20Element%20in%20an%20Array2.png\" alt=\"\" \/><\/p>\n<ol start=\"3\">\n<li>Now we will iterate on the remaining array, now we are in element 11 which is smaller than the top element of the priority queue i.e, 49 so we will remove 49 and add 11 to the priority queue.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203613-Kth%20Smallest%20Element%20in%20an%20Array3.png\" alt=\"\" \/><\/p>\n<ol start=\"4\">\n<li>Now, we are on element 73 which is greater than the top element of the priority queue i.e, 42 so we will now do anything same with 85.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203621-Kth%20Smallest%20Element%20in%20an%20Array4.png\" alt=\"\" \/><\/p>\n<ol start=\"5\">\n<li>Now element 39 is smaller than the top element of the priority queue i.e, 42 so we will remove 42 and insert 39.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203621-Kth%20Smallest%20Element%20in%20an%20Array5.png\" alt=\"\" \/><\/p>\n<ol start=\"6\">\n<li>Now our array iteration is over and our priority queue is looking like this hence our answer is the top element of the priority queue i.e, 39.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203622-Kth%20Smallest%20Element%20in%20an%20Array6.png\" alt=\"\" \/><\/p>\n<p><strong>Code Implementation<\/strong><br \/>\nNow, we will see the implementation of the above approach in various programming languages.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12474 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12474 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12474 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12474 .wpsm_nav-tabs > li.active > a, #tab_container_12474 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12474 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12474 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12474 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12474 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12474 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12474 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12474 .wpsm_nav-tabs > li > a:hover , #tab_container_12474 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12474 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12474 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12474 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12474 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12474 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12474 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12474 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12474 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12474 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12474 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12474 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12474\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12474\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12474_1\" aria-controls=\"tabs_desc_12474_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12474_2\" aria-controls=\"tabs_desc_12474_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12474_3\" aria-controls=\"tabs_desc_12474_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12474\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12474_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\nint kthSmallestelem(int arr[], int N, int K)\r\n{\r\n\r\n    priority_queue&lt;int&gt; pq;\r\n\r\n    for (int i = 0; i &lt; K; i++) {\r\n        pq.push(arr[i]);\r\n    }\r\n    for (int i = K; i &lt; N; i++) {\r\n\r\n        if (arr[i] &lt; pq.top()) {\r\n            pq.pop();\r\n            pq.push(arr[i]);\r\n        }\r\n    }\r\n\r\n    return pq.top();\r\n}\r\n\r\nint main()\r\n{\r\n    int N = 7;\r\n    int arr[N] = { 42, 49, 12, 11, 73, 85, 39 };\r\n    int K = 3;\r\n\r\n    cout &lt;&lt; \"Kth Smallest Element is: \"\r\n        &lt;&lt; kthSmallestelem(arr, N, K);\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12474_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.util.*;\r\nclass MinHeapComparator implements Comparator&lt;Integer&gt; {\r\n\r\n    public int compare(Integer number1, Integer number2)\r\n    {\r\n        int value = number1.compareTo(number2);\r\n\r\n        if (value &gt; 0) {\r\n            return -1;\r\n        }\r\n        else if (value &lt; 0) {\r\n            return 1;\r\n        }\r\n        else {\r\n            return 0;\r\n        }\r\n    }\r\n}\r\nclass GFG {\r\n\r\n    static int kthSmallestelem(int[] v, int N, int K)\r\n    {\r\n    \r\n        PriorityQueue&lt;Integer&gt; heap1\r\n            = new PriorityQueue&lt;Integer&gt;(\r\n                new MinHeapComparator());\r\n\r\n        for (int i = 0; i &lt; N; ++i) {\r\n\r\n            heap1.add(v[i]);\r\n\r\n            if (heap1.size() &gt; K) {\r\n                heap1.remove();\r\n            }\r\n        }\r\n\r\n        return heap1.peek();\r\n    }\r\n\r\n    public static void main(String[] args)\r\n    {\r\n        int[] vec\r\n            = { 42, 49, 12, 11, 73, 85, 39 };\r\n\r\n        int N = vec.length;\r\n\r\n        int K = 3;\r\n\r\n        System.out.println(\"Kth Smallest Element: \"\r\n                        + kthSmallestelem(vec, N, K));\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_12474_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import heapq\r\n\r\n\r\n\r\ndef kthSmallest(arr, N, K):\r\n\r\n    pq = []\r\n    for i in range(K):\r\n\r\n        heapq.heappush(pq, arr[i])\r\n        heapq._heapify_max(pq)\r\n\r\n    for i in range(K, N):\r\n\r\n        \r\n        if arr[i] &lt; pq[0]:\r\n            heapq.heappop(pq)\r\n\r\n            # Push curr element\r\n            heapq.heappush(pq, arr[i])\r\n            heapq._heapify_max(pq)\r\n    return pq[0]\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    N = 7\r\n    arr = [42, 49, 12, 11, 73, 85, 39 ]\r\n    K = 3\r\n\r\n    print(\"Kth Smallest Element is:\", kthSmallest(arr, N, K))\r\n\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12474 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12474 a\"),jQuery(\"#tab-content_12474\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>Kth Smallest Element is: 39<\/code><\/pre>\n<p><strong>Explanation of the above code<\/strong><br \/>\nIn the above code, we have used the priority queue to find the kth smallest element in an array by following the algorithm explained above.<\/p>\n<p><strong>Time Complexity<\/strong><br \/>\nThe time complexity of the above code is O(K log K +  (N \u2013 K) log K) as we are using a priority queue.<\/p>\n<p><strong>Space Complexity<\/strong><br \/>\nThe space complexity of the above code is O(K) as we are storing K elements in the priority queue.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nArrays are one of the most used data structures in any programming language and above we have discussed one of the asked questions from the array topic, i.e, find the kth smallest element in an array, we have discussed numerous approaches of the above problem and with them, we have also discussed the algorithm and code implementation with output and explanation of the same.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<p><strong>1. Explain list vs array in programming languages.<\/strong><br \/>\nAn array is a data structure that stores a collection of elements, each identified by an index or a key. A list is a data structure that stores an ordered collection of elements.<\/p>\n<p><strong>2. What is the time complexity of finding the Kth smallest element in an array?<\/strong><br \/>\nThe time complexity of finding the Kth smallest element in an array depends on the algorithm used. Quickselect has an average time complexity of O(n), while heap sort has a time complexity of O(nlogn). Binary search has a time complexity of O(logn).<\/p>\n<p><strong>3. Can the Kth smallest element in an array be found in a sorted array?<\/strong><br \/>\nYes, the Kth smallest element in a sorted array can be found simply by selecting the Kth element in the array.<\/p>\n<p><strong>4. What is the difference between the Kth largest and Kth smallest element in an array?<\/strong><br \/>\nThe Kth largest element in an array is the element that would be at the (n-k+1)th position if the array were sorted in descending order, where n is the number of elements in the array. The Kth smallest element in an array, on the other hand, is the element that would be at the Kth position if the array were sorted in ascending order.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An array is a data structure used in computer programming to store a collection of values, each with its own unique index or key. These values can be of any data type, such as integers, characters, or objects. Arrays allow for efficient access to and manipulation of individual elements, making them a common tool for [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[163],"tags":[],"class_list":["post-12759","post","type-post","status-publish","format-standard","hentry","category-arrays"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Find Kth Smallest Element in an Array<\/title>\n<meta name=\"description\" content=\"Methods to find Kth smallest element by using Brute Force Sorting, Min Heap, MaxHeap, Quick Select, Map and Frequency of Elements and Priority Queue\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Find Kth Smallest Element in an Array\" \/>\n<meta property=\"og:description\" content=\"Methods to find Kth smallest element by using Brute Force Sorting, Min Heap, MaxHeap, Quick Select, Map and Frequency of Elements and Priority Queue\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-13T10:16:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-19T11:36:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Kth Smallest Element in an Array\",\"datePublished\":\"2023-02-13T10:16:45+00:00\",\"dateModified\":\"2023-04-19T11:36:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/\"},\"wordCount\":2357,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg\",\"articleSection\":[\"Arrays\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/\",\"name\":\"Find Kth Smallest Element in an Array\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg\",\"datePublished\":\"2023-02-13T10:16:45+00:00\",\"dateModified\":\"2023-04-19T11:36:39+00:00\",\"description\":\"Methods to find Kth smallest element by using Brute Force Sorting, Min Heap, MaxHeap, Quick Select, Map and Frequency of Elements and Priority Queue\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Arrays\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/arrays\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Kth Smallest Element in an Array\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Find Kth Smallest Element in an Array","description":"Methods to find Kth smallest element by using Brute Force Sorting, Min Heap, MaxHeap, Quick Select, Map and Frequency of Elements and Priority Queue","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:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/","og_locale":"en_US","og_type":"article","og_title":"Find Kth Smallest Element in an Array","og_description":"Methods to find Kth smallest element by using Brute Force Sorting, Min Heap, MaxHeap, Quick Select, Map and Frequency of Elements and Priority Queue","og_url":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-02-13T10:16:45+00:00","article_modified_time":"2023-04-19T11:36:39+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Kth Smallest Element in an Array","datePublished":"2023-02-13T10:16:45+00:00","dateModified":"2023-04-19T11:36:39+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/"},"wordCount":2357,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg","articleSection":["Arrays"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/","url":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/","name":"Find Kth Smallest Element in an Array","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg","datePublished":"2023-02-13T10:16:45+00:00","dateModified":"2023-04-19T11:36:39+00:00","description":"Methods to find Kth smallest element by using Brute Force Sorting, Min Heap, MaxHeap, Quick Select, Map and Frequency of Elements and Priority Queue","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676283203526-Kth%20Smallest%20Element%20in%20an%20Array.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/kth-smallest-element-in-an-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Arrays","item":"https:\/\/prepbytes.com\/blog\/category\/arrays\/"},{"@type":"ListItem","position":3,"name":"Kth Smallest Element in an Array"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=12759"}],"version-history":[{"count":4,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12759\/revisions"}],"predecessor-version":[{"id":15793,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12759\/revisions\/15793"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=12759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=12759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=12759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}