Skip to main content
Back to problems
Leetcode
Medium
Math
Number Theory
Greedy
The Kth Factor Of N

Find the k-th positive divisor of an integer n in increasing order, or report that it does not exist.

Acceptance 0%
Problem Statement

Given two integers nn and kk, consider all positive factors (divisors) of nn arranged in strictly increasing order. Return the factor that appears at position kk in this ordering.

If nn has fewer than kk positive factors, return 1-1.

Your goal is to determine the answer efficiently without explicitly relying on a large, repeated search over all numbers up to nn.

Input Format

  • The input consists of two integers nn and kk.
  • nn is the number whose positive factors are being queried.
  • kk is the 1-indexed position of the factor to return.

Output Format

  • Return a single integer: the kk-th positive factor of nn in increasing order.
  • If fewer than kk factors exist, return 1-1.

Constraints

  • 1n1 \le n and 1k1 \le k.
  • The answer is 1-indexed.
  • Factors are positive integers that divide nn evenly.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 12, k = 3

Output

3

Explanation

The positive factors of 12 in increasing order are [1, 2, 3, 4, 6, 12]. The 3rd factor is 3.

Example 2

Input

n = 7, k = 2

Output

7

Explanation

The positive factors of 7 are [1, 7]. The 2nd factor is 7.

Show 1 more example

Example 3

Input

n = 4, k = 4

Output

-1

Explanation

The positive factors of 4 are [1, 2, 4], so there is no 4th factor.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.