Advertisement

The kth Factor of n - LeetCode 1492 Solution

The kth Factor of n - Complete Solution Guide

The kth Factor of n is LeetCode problem 1492, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two positive integers n and k . A factor of an integer n is defined as an integer i where n % i == 0 . Consider a list of all factors of n sorted in ascending order , return the k th 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 3 rd factor is 3. Example 2: Input: n = 7, k = 2 Output: 7 Explanation: Factors list is [1, 7], the 2 nd factor is 7. Example 3: Input: n = 4, k

Detailed Explanation

The problem asks us to find the k-th factor of a given number 'n'. A factor is a number 'i' that divides 'n' without leaving a remainder (n % i == 0). We need to find all factors of 'n', sort them in ascending order, and return the k-th element in this sorted list. If 'n' has fewer than 'k' factors, we should return -1.

Solution Approach

The solution iterates from 1 up to the square root of 'n'. For each number 'i' in this range, it checks if 'i' is a factor of 'n'. If it is, 'i' is added to a list of 'small_factors'. If 'i * i' is not equal to 'n' (to avoid duplicates for perfect squares), 'n / i' is added to a list of 'large_factors'. After the loop, the 'large_factors' list is reversed and appended to the 'small_factors' list to form the complete list of factors in ascending order. Finally, it checks if 'k' is within the bounds of the factor list and returns the k-th factor (k-1 because of 0-based indexing) or -1 if 'k' is out of bounds. The Python solution does this without explicit lists, further optimizing space.

Step-by-Step Algorithm

  1. Step 1: Calculate the square root of 'n'.
  2. Step 2: Iterate from 'i' = 1 to the square root of 'n'.
  3. Step 3: If 'i' is a factor of 'n' (n % i == 0), check if k is one. If it is, return the value of i, and decrement k
  4. Step 4: If 'i * i' is equal to 'n' (meaning n is a perfect square), then skip the next step in the first loop.
  5. Step 5: Second Loop is from sqrt(n) downto 1, check if the current i squared equals n. If so, continue the current loop iteration.
  6. Step 6: check if n is divisible by i. If so, decrement k. If k is 0, return n divided by i
  7. Step 7: If the loop finishes without finding the k-th factor, return -1.

Key Insights

  • Insight 1: We only need to iterate up to the square root of 'n' to find all factors. If 'i' is a factor of 'n', then 'n/i' is also a factor.
  • Insight 2: We can optimize space by not explicitly storing all factors in a single list initially. Instead, we can keep track of factors less than or equal to the square root and factors greater than the square root separately and then combine them. The Python solution avoids explicit list creation.
  • Insight 3: We need to handle the case where 'n' is a perfect square (e.g., n = 9). In this case, the square root is counted only once as a factor.

Complexity Analysis

Time Complexity: O(sqrt(n))

Space Complexity: O(1)

Topics

This problem involves: Math, Number Theory.

Companies

Asked at: Expedia, IBM.