Created
February 11, 2022 01:23
-
-
Save amarjitdhillon/ed318e17be3e09edf52b8e175593a692 to your computer and use it in GitHub Desktop.
The kth Factor of n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def kthFactor(self, n: int, k: int) -> int: | |
# counter represent the i^th factor of the number found so far | |
counter = 0 | |
# go over the number from 1 to n as we need to divide the n by this number to find if that number is a factor | |
for i in range(1, n+1): | |
if n%i == 0: | |
# increment the value of counter | |
counter += 1 | |
# if the counter is equal to k, it means we have found the k^th factor | |
if k == counter: | |
return i | |
# if no factor is found then return -1 | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment