class Solution:
    def kthFactor(self, n: int, k: int) -> int:
        """ tc O(sqrt(N)) sc O(sqrt(N))
        1. set i upper bound sqrt(n)
        2. keep incrementing i up to upperbound
            2.1 check if i is factor of n  
                                        if yes, check if n//i is same as i : if divisor is different from i, same divisor to a list 
            2.2 add cnt by 1, check if cnt reaches k, if yes, return i 
        3. check if cnt + len(list of divisor)  <  k, if yes => no result; otherwise result falls
        within (k-cnt) th number in the divisor list.  

        since the list is sorted in descending order, get the last (k-cnt) th number 
        
        """
        size = int(sqrt(n))
        cnt = 0
        l = []
        i = 1
        while i * i <= n:
            if n%i == 0:
                if i*i != n:
                    l.append(n//i)
                cnt+= 1 
                if cnt==k:
                    return i
            i += 1
        if len(l) + cnt < k:
            return -1
        return l[- (k-cnt)]