Skip to content

LeetCode 1492 - The kth Factor of n

Difficulty: medium

Table of Contents

Open Table of Contents

Problem

Given two positive integers n and k, find the kth factor of n in ascending order. A factor of an integer n is defined as an integer i where n % i == 0. Return the kth factor in this list or return -1 if n has less than k factors.

Example 1:

Input: n = 12, k = 3
Output: 3
Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.

Constraints:

Follow up: Could you solve this problem in less than O(n) complexity?

Approaches

Brute Force Approach:

  1. Iterate Through Numbers: Iterate from 1 to n, checking if each number is a factor of n.
  2. Count Factors: Maintain a count of factors. If a factor is found, increment the count.
  3. Check kth Factor: When the count reaches k, return the current number. If k is greater than the number of factors, return -1.

Optimized Approach:

  1. Iterate with Early Stopping: Iterate from 1 to the square root of n. This is because factors beyond the square root of n are complements of the factors before it.
  2. Count and Store Factors: Maintain a count and a list of factors found. For each factor i, also consider n / i as a factor.
  3. Determine kth Factor: If k is within the range of factors found, return the kth factor. Otherwise, return -1.

Code Implementation

Brute Force

def kthFactor(n, k):
    count = 0
    for i in range(1, n + 1):
        if n % i == 0:
            count += 1
            if count == k:
                return i
    return -1

Optimized

def kthFactor(n, k):
    # Initialize an empty list to store the factors of n
    factors = []

    # Iterate through numbers from 1 to the square root of n (inclusive)
    for i in range(1, int(n**0.5) + 1):
        # Check if i is a factor of n
        if n % i == 0:
            # If i is a factor, add it to the list of factors
            factors.append(i)
            # Check if the corresponding factor (n // i) is different from i
            if i != n // i:
                # If it is different, add the corresponding factor to the list
                factors.append(n // i)

    # Sort the list of factors in ascending order
    factors.sort()

    # Check if the list of factors is long enough to have a k-th element
    if k <= len(factors):
        # If it is, return the k-th factor (considering 1-based indexing)
        return factors[k - 1]

    # If the k-th factor does not exist, return -1
    return -1

Brute Force:

Optimized:

Visual Explanation

Step 1: Initialize Factors List

factors = []

Step 2: Iterate to Find Factors

Step 3: Check and Add Factors

Iteration 1: i = 1

factors = [1, 30]

Iteration 2: i = 2

factors = [1, 30, 2, 15]

Iteration 3: i = 3

factors = [1, 30, 2, 15, 3, 10]

Iteration 4: i = 4

Iteration 5: i = 5

factors = [1, 30, 2, 15, 3, 10, 5, 6]

Step 4: Sort the Factors

factors = [1, 2, 3, 5, 6, 10, 15, 30]

Step 5: Select the k-th Factor

4th factor = factors[3] = 5

In-detail explanation of Optimized Solution

The function is designed to find all factors of a given number n. A factor is a number that divides n without leaving a remainder. For every factor i found, there could potentially be a corresponding factor, which is n // i.

The Line if i != n // i:

Detailed Explanation

Example

Let’s take n = 12 as an example:

The line if i != n // i: ensures that each factor is only added once to the list, even in cases where n is a perfect square or when two factors are numerically equal. This is crucial for correctly counting and listing distinct factors of n.