{"id":6972,"date":"2020-06-27T11:14:42","date_gmt":"2020-06-27T18:14:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6972"},"modified":"2020-06-27T11:15:12","modified_gmt":"2020-06-27T18:15:12","slug":"leetcode-1492-the-kth-factor-of-n","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1492-the-kth-factor-of-n\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1492. The kth Factor of n"},"content":{"rendered":"\n<p>Given two positive integers&nbsp;<code>n<\/code>&nbsp;and&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>A factor of an integer&nbsp;<code>n<\/code>&nbsp;is defined as an integer&nbsp;<code>i<\/code>&nbsp;where&nbsp;<code>n % i == 0<\/code>.<\/p>\n\n\n\n<p>Consider a list of all factors of&nbsp;<code>n<\/code>&nbsp;sorted in&nbsp;<strong>ascending order<\/strong>, return&nbsp;<em>the&nbsp;<\/em><code>kth<\/code><em>&nbsp;factor<\/em>&nbsp;in this list or return&nbsp;<strong>-1<\/strong>&nbsp;if&nbsp;<code>n<\/code>&nbsp;has less than&nbsp;<code>k<\/code>&nbsp;factors.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 12, k = 3\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7, k = 2\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> Factors list is [1, 7], the 2nd factor is 7.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, k = 4\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> Factors list is [1, 2, 4], there is only 3 factors. We should return -1.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 1, k = 1\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Factors list is [1], the 1st factor is 1.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 1000, k = 3\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> Factors list is [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500, 1000].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= k &lt;= n &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int kthFactor(int n, int k) {\n    for (int i = 1; i <= n; ++i)\n      if (n % i == 0 &#038;&#038; --k == 0) return i;\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two positive integers&nbsp;n&nbsp;and&nbsp;k. A factor of an integer&nbsp;n&nbsp;is defined as an integer&nbsp;i&nbsp;where&nbsp;n % i == 0. Consider a list of all factors of&nbsp;n&nbsp;sorted in&nbsp;ascending&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[441,177],"class_list":["post-6972","post","type-post","status-publish","format-standard","hentry","category-array","tag-factor","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6972"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6972\/revisions"}],"predecessor-version":[{"id":6974,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6972\/revisions\/6974"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}