Kth Positive Factor in Ascending Order

Quick Overview

Kth Positive Factor in Ascending Order. Return the kth smallest positive divisor of n,or-1 if n has fewer than k positive divisors.

Kth Positive Factor in Ascending Order

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the kth smallest positive divisor of n,or -1 if n has fewer than k positive divisors. ```cpp class Solution { public: int kthFactor(int n, int k); }; ``` ### Examples **Example 1:** ```text Input: n = 18, k = 4 Output: 6 Explanation: The factors are1,2,3,6,9,18. ``` **Example 2:** ```text Input: n = 9, k = 4 Output: -1 Explanation: Its factors are only1,3,9; the square-root factor3 is counted once. ``` ### Constraints - `1 <= k <= n <= 1000`. ### Contract considerations Only positive factors are included. ```hint Read the boundary condition Count only positive factors, in ascending order and without duplication. Check perfect squares and a requested rank beyond the factor count. ``` ### Boundary reminders Only positive factors are included. ### Optional extensions Can you solve the problem in less than O(n) time?

Overview: Kth Positive Factor in Ascending Order. Return the kth smallest positive divisor of n,or-1 if n has fewer than k positive divisors.

Amazon logo
Amazon
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the kth smallest positive divisor of n,or -1 if n has fewer than k positive divisors.

class Solution {
public:
    int kthFactor(int n, int k);
};

Examples

Example 1:

Input: n = 18, k = 4
Output: 6
Explanation: The factors are1,2,3,6,9,18.

Example 2:

Input: n = 9, k = 4
Output: -1
Explanation: Its factors are only1,3,9; the square-root factor3 is counted once.

Constraints

  • 1 <= k <= n <= 1000 .

Contract considerations

Only positive factors are included.

Boundary reminders

Only positive factors are included.

Optional extensions

Can you solve the problem in less than O(n) time?