1492. The kth Factor of n - Detailed Explanation

Problem Statement

Given two positive integers n and k, a factor of n is a positive integer that divides n evenly (i.e. without leaving a remainder). The task is to find the kth factor of n when the factors are listed in ascending order. If n has fewer than k factors, return -1.

Example 1

  • Input: n = 12, k = 3
  • Explanation:
    The factors of 12 are: [1, 2, 3, 4, 6, 12].
    The 3rd factor is 3.
  • Output: 3

Example 2

  • Input: n = 7, k = 2
  • Explanation:
    The factors of 7 are: [1, 7].
    The 2nd factor is 7.
  • Output: 7

Example 3

  • Input: n = 4, k = 4
  • Explanation:
    The factors of 4 are: [1, 2, 4].
    There is no 4th factor.
  • Output: -1

Constraints

  • 1 ≤ n ≤ 1000
  • 1 ≤ k ≤ n

Hints to Guide Your Approach

  1. Understand the Factor Definition:
    A factor divides the number evenly, so for any integer i, if n % i == 0, then i is a factor of n.

  2. Order Matters:
    Factors are considered in ascending order. You could either iterate from 1 to n and count each factor or collect them (perhaps using a √n optimization) and sort them.

  3. Optimizations:
    Although you could simply loop from 1 to n, you can reduce the number of iterations by noticing that factors come in pairs (if i is a factor, so is n/i). Be cautious to keep them in sorted order.

Approaches to Solve the Problem

Approach 1: Simple Iteration (Brute Force)

Explanation

  • Idea:
    Loop from 1 through n. Each time you find a divisor (i.e. n % i == 0), count it. When the count reaches k, return that number.

  • Steps:

    1. Initialize a counter.
    2. For each integer i from 1 to n, check if i divides n evenly.
    3. Increment the counter for every factor found.
    4. When the counter equals k, return i.
    5. If you complete the loop without reaching k, return -1.
  • Complexity Analysis:

    • Time Complexity: O(n)
    • Space Complexity: O(1)

Approach 2: Optimized Iteration Using √n

Explanation

  • Idea:
    Every factor i of n has a corresponding factor n/i. You can iterate only up to √n and collect factors in two lists—one for factors found directly (in ascending order) and one for their paired factors (which will be in descending order). Then, merge the lists carefully to get the complete sorted order.

  • Steps:

    1. Initialize two lists: one for small factors (from 1 to √n) and one for their corresponding large factors.

    2. Iterate i from 1 to √n.

      • If n % i == 0, add i to the small factors list.
      • If i is not equal to n/i (to avoid duplicates, especially when n is a perfect square), add n/i to the large factors list.
    3. Reverse the large factors list (since it will be in descending order).

    4. Concatenate the small factors list with the reversed large factors list.

    5. Check if the total number of factors is at least k; if yes, return the kth factor (1-indexed), otherwise return -1.

  • Complexity Analysis:

    • Time Complexity: O(√n + f log f) where f is the number of factors (but f is small compared to n)

    • Space Complexity: O(f), which is acceptable since the number of factors is low.

Code Implementations

Python Code

Below is the Python implementation using the simple iteration approach. (The √n approach can be implemented similarly.)

Python3
Python3

. . . .

Java Code

Below is the Java implementation using the simple iteration approach.

Java
Java

. . . .

Step-by-Step Walkthrough & Visual Examples

Walkthrough for n = 12, k = 3

  1. Initialization:
    Set count = 0.

  2. Iteration:

    • i = 1:
      12 % 1 == 0 → factor found. Increment count to 1.
      (1st factor: 1)

    • i = 2:
      12 % 2 == 0 → factor found. Increment count to 2.
      (2nd factor: 2)

    • i = 3:
      12 % 3 == 0 → factor found. Increment count to 3.
      (3rd factor: 3)
      Since count equals k (3), return 3.

Visual Representation

Factors of 12: 1, 2, 3, 4, 6, 12
            Order:  1st, 2nd, 3rd, 4th, 5th, 6th
k = 3, so the 3rd factor is 3.

Additional Sections

Common Mistakes

  • Off-by-One Errors:
    Ensure that you start counting factors from 1 (not 0) and correctly return the kth factor.
  • Improper Handling of Edge Cases:
    For example, when n = 1, the only factor is 1; if k > 1, you must return -1.

Edge Cases

  • n = 1:
    Only one factor exists (1). For any k > 1, the answer should be -1.

  • k Larger Than Number of Factors:
    If the number of factors is less than k (e.g., n = 4, factors = [1,2,4] and k = 4), return -1.

  • Variations:
    • Find the kth factor in descending order.

    • Return all factors instead of just the kth.

  • Related Problems for Further Practice:
    • “Count Factors of an Integer”
    • “Find All Factors of a Number”

Complexity Recap

  • Simple Iteration Approach:
    • Time Complexity: O(n)

    • Space Complexity: O(1)

  • Optimized √n Approach (if implemented):
    • Time Complexity: O(√n) (plus sorting overhead for merging factors)
    • Space Complexity: O(f) where f is the number of factors (typically much smaller than n)
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
1041. Robot Bounded In Circle - Detailed Explanation
Learn to solve Leetcode 1041. Robot Bounded In Circle with multiple approaches.
2109. Adding Spaces to a String - Detailed Explanation
Learn to solve Leetcode 2109. Adding Spaces to a String with multiple approaches.
27. Remove Element - Detailed Explanation
Learn to solve Leetcode 27. Remove Element with multiple approaches.
240. Search a 2D Matrix II - Detailed Explanation
Learn to solve Leetcode 240. Search a 2D Matrix II with multiple approaches.
994. Rotting Oranges - Detailed Explanation
Learn to solve Leetcode 994. Rotting Oranges with multiple approaches.
652. Find Duplicate Subtrees - Detailed Explanation
Learn to solve Leetcode 652. Find Duplicate Subtrees with multiple approaches.
Related Courses
Course image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
Discounted price for Your Region

$197

Course image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$78

Course image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Image
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.