Skip to content

Commit

Permalink
Add 1492_The_kth_Factor_of_n.java
Browse files Browse the repository at this point in the history
  • Loading branch information
seanprashad committed Mar 21, 2021
1 parent 807dffc commit cbaa0d5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Math/1492_The_kth_Factor_of_n.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int kthFactor(int n, int k) {
List<Integer> l = new ArrayList<>();

for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (i * i != n) {
l.add(n / i);
}

if (--k == 0) {
return i;
}
}
}

if (l.size() < k) {
return -1;
}
return l.get(l.size() - k);
}
}

0 comments on commit cbaa0d5

Please sign in to comment.